How to loop through each byte in a Ruby String

Problem: You need to loop through each character in a Ruby String, and get the byte value of the character as you iterate through the characters in the String.

Solution: Just use the each_byte method of the String class to loop through each byte in your String, like this:

a = 'hello, world'
a.each_byte { |c|
  puts c
}

This results in the following output:

104
101
108
108
111
44
32
119
111
114
108
100