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

eg:

logan:/Users/logan% irb
irb(main):001:0> str = "Hello, world!"
=> "Hello, world!"
irb(main):002:0> str.split(//).each do |character|
irb(main):003:1*    puts character
irb(main):004:1> end
H
e
l
l
o
,
w
o
r
l
d
!
=> ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]

if the extraneous typing bothers you, you can always add it to String.

class String
     def each_char(&block)
            split(//).each(&block)
            self
     end
end