Ruby: character to ascii from a string Ruby: character to ascii from a string ruby ruby

Ruby: character to ascii from a string


The c variable already contains the char code!

"string".each_byte do |c|    puts cend

yields

115116114105110103


puts "string".split('').map(&:ord).to_s


Ruby String provides the codepoints method after 1.9.1.

str = 'hello world'str.codepoints.to_a=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] str = "你好世界"str.codepoints.to_a=> [20320, 22909, 19990, 30028]