Home > Articles > Programming > Ruby

📄 Contents

  • Print
  • + Share This
This chapter is from the book

2.6 Processing a Byte at a Time

Because Ruby is not fully internationalized at the time of this writing, a character is essentially the same as a byte. To process these in sequence, use the each_byte iterator:

str = "ABC"
str.each_byte {|char| print char, " " }
# Produces output: 65 66 67

In current versions of Ruby, you can break a string into an array of one-character strings by using scan with a simple wildcard regular expression matching a single character:

str = "ABC"
chars = str.scan(/./)
chars.each {|char| print char, " " }
# Produces output: A B C
  • + Share This
  • 🔖 Save To Your Account