Hi,

On Sat, Dec 23, 2000 at 07:10:03PM +0900, Jiangyi Liu wrote:
> Hi all,
> 
> I met a problem when I tried to write some Ruby code to learn Ruby. The
> following code just can't work as expected,
> 
> "Hello".each_byte { |c|
>     if c == "o"
>         print "oh, i got an o!", "\n"
>     end
> }

String#each_byte returns the (Integer) value of the character, not a string 
or the character itself.  So, you can use the Integer#chr method to convert
the value to a string and compare that:

"Hello".each_byte { |c|
    if c.chr == "o"
        print "oh, i got an o!", "\n"
    end
}

You can also compare c to 111 (the ASCII value of 'o'), but this is clearer,
IMO.


> Seems in Ruby, in the comparsion c and "o" is always false. How can I make
> the above code right? Thanks a lot.
Welcome :-)

Arjen

-- 
Arjen Laarhoven                         |
Email: Home: arjen / aragorn.demon.nl     |        
       Work: arjen / murphy.nl            | 
Web: http://www.aragorn.demon.nl        | All that is gold does not glitter...