On Dec 20, 2005, at 6:21 AM, Mark Hubbart wrote:

> On 12/20/05, Logan Capaldo <logancapaldo / gmail.com> wrote:
>>
>> On Dec 20, 2005, at 4:52 AM, shinya wrote:
>>
>>> Hi there!
>>> I'm a ruby newbie, and I'm searching for a way to iterate every
>>> char in a string, but I cannot find any easy way. My problem is to
>>> look at every char in a string and match it with some known letter.
>>> I use the String#each_byte iterator for now, but it still be a poor
>>> solution :/
>>> Thanks,
>>>
>>> shinya.
>>>
>>
>> The usual idiom is str.split(//).each do |character|
>>                 # do stuff with character
>>            end
>
> String#scan with a block is lighter weight, and less wordy:
> str.scan(/./) do |character|
>   # stuff
> end

str = "Hello\nWorld"

str.scan(/./) do |character|
     p character
end

irb(main):015:0> str.scan(/./) do |character|
irb(main):016:1*   p character
irb(main):017:1> end
"H"
"e"
"l"
"l"
"o"
"W"
"o"
"r"
"l"
"d"

Where'd my newlines go? :(

irb(main):021:0> str.split(//).each do |character|
irb(main):022:1*  p character
irb(main):023:1> end
"H"
"e"
"l"
"l"
"o"
"\n"
"W"
"o"
"r"
"l"
"d"

If you want to use scan, you should use scan(/./m)

irb(main):024:0> str.scan(/./m) do |character|
irb(main):025:1*   p character
irb(main):026:1> end
"H"
"e"
"l"
"l"
"o"
"\n"
"W"
"o"
"r"
"l"
"d"