Ruby字节数组转换为十六进制字符串

Python011

Ruby字节数组转换为十六进制字符串,第1张

str = "Ruby"

str.split(//).each {|e|print (e.unpack('H*').to_s + "\n")}

#irb

# 52

# 75

# 62

# 79

# =>["R", "u", "b", "y"]

计算文件大小的不会,给你看看行数计算吧

Ruby文件行数计算代码示例:

module Enumerable

# function to get total lines for file

def total_lines

lines = 0

each_with_index {|content,lines|}

return lines+1

end

end

class CheckLines

require 'find'

@check_type = %w{txt rb erb yml html css xml}

def initialize(directory)

@total_lines = 0

if File.directory?(directory)

@directorydirectory = directory

@contents = {}

self.go

else puts "#{directory} is not a directory! check it out!" and return

end

end

def go

if @directory

Find.find @directory do |path|

pathpathlite = path.gsub(@directory,'')

if File.file? path

File.open path do |f|

tmp_line = f.total_lines

@contents.store(pathlite,tmp_line)

@total_lines += tmp_line

end

end

end

puts @total_lines

end

end

def details

@contents.each do |key,value|

puts "#{key} file has lines of #{value}"

end

end

end