You're trying something completely wrong here. The ? is only a compiler
thing and not an operator. If you write ?letter, it's wrong because ?l
can be interpreted as the code of the letter l, and the remaining etter
is rubbish. It is not a real operator!
Apart from that, your each doesn't split the string into letters, but
into lines.
In Ruby 1.8 if you want the code of n-th character of your str, just
write str[n]. It doesn't return a single character, but its code.
In Ruby 1.9 str[n] returns n-th character in the form of a short string,
and to get code of n-th letter, write str[n].ord.
Thank you. I did understand that the ?letter was actually taking the 'l'
and would have worked on it, as it should (had it not been for the 'etter'
directly afterward. I just was trying to find out how to send ? something
in an iterator. Bottom line: There are better ways to accomplish this.
(Oops, on the lack of hi.split(//) or .split(/./) ) I understand that, and
didn't catch it as I was writing the IRB of something I knew was going to
fail.
It was just something I came across, and hadn't seen it used in anything, so
was wondering the uses.
···
On Sun, Sep 7, 2008 at 12:15 PM, Thomas B. <tpreal@gmail.com> wrote:
Victor Goff wrote:
>>> hi.each {|letter| puts ?letter }
You're trying something completely wrong here. The ? is only a compiler
thing and not an operator. If you write ?letter, it's wrong because ?l
can be interpreted as the code of the letter l, and the remaining etter
is rubbish. It is not a real operator!
Apart from that, your each doesn't split the string into letters, but
into lines.
In Ruby 1.8 if you want the code of n-th character of your str, just
write str[n]. It doesn't return a single character, but its code.
In Ruby 1.9 str[n] returns n-th character in the form of a short string,
and to get code of n-th letter, write str[n].ord.