"I am puzzled".each_byte do |b| puts "%c" % b ;end
Cheers
Robert
···
On 3/20/06, James Edward Gray II <james@grayproductions.net> wrote:
On Mar 20, 2006, at 3:38 AM, Robert Klemme wrote:
> Kev Jackson wrote:
>> Robert Klemme wrote:
>>> Lyndon Samson wrote:
>>>
>>>> a="123"
>>>>
>>>> 0.upto(a.length) { |i| puts a[i..i] }
>>>
>>>
>>> Alternatively:
>>>
>>> a.length.times {|i| puts a[i].chr}
>>>
>>> Many roads to Rome...
>>>
>>> robert
>>>
>> or even
>> a.scan(/./) { |c| p c }
>
> We had that already: your version ignores newlines.
I'm surprised that not many people knew about 'each_byte()'. Maybe it's a problem with Ruby docs? Or maybe it is just counter-intuitive - I would expect each() iterate over bytes, and provide each_lines() to iterate over lines instead.
each_bytes is not a good way to do this, btw. It will not remain
compatibile with Ruby 2.0. And using enumerator is a pretty heavy
solution. Then there's this...
require 'facet/string/chars'
Source code for chars.rb:
class String
# Returns an array of characters.
···
#
# "abc".chars #=> ["a","b","c"]
#
def chars
self.split(//)
end