Section 2.7. Performing Specialized String Comparisons


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





The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net