J
John
I have seen a lot of people having trouble with String not including an
iterator for each character, and rather just each byte. I include this
snippet in any code that needs to iterate over each character in a
string. Simple, elegant, and very very Ruby! Man, I love redefining
pre-existing classes.
# Now you can use the syntax:
# "foobar".each_char do ...
class String
def each_char
each_byte { |byte| yield byte.chr }
end
end
iterator for each character, and rather just each byte. I include this
snippet in any code that needs to iterate over each character in a
string. Simple, elegant, and very very Ruby! Man, I love redefining
pre-existing classes.
# Now you can use the syntax:
# "foobar".each_char do ...
class String
def each_char
each_byte { |byte| yield byte.chr }
end
end