I have been looking at this little anomoly ?A => 65

I know that the reverse is 65.chr
but I can find no documentation for this (aside from the above line)

And I wanted to find out more about it, so am trying things like:>> ?A
=> 65

hi = "Hello World"

=> "Hello World"

hi.each {|letter| puts ?letter }

SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
        from (irb):2

hi.each {|letter| puts ?"#{letter}" }

^C

hi

=> "Hello World"

hi.each {|letter| puts letter.? }

(irb):5: warning: invalid character syntax; use ?\s
SyntaxError: compile error
(irb):5: syntax error, unexpected '?'
hi.each {|letter| puts letter.? }
                               ^
        from (irb):5

hi.each {|letter| puts letter.?\s }

SyntaxError: compile error
(irb):6: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s }
                                 ^
        from (irb):6

hi.each {|letter| puts letter.?\s65 }

SyntaxError: compile error
(irb):7: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s65 }
                                 ^
        from (irb):7

Try hi.each {|l| p [l, l.class]} to see why it does not work.

Kind regards

robert

···

2008/9/7 Victor H. Goff III <keeperotphones@gmail.com>:

I know that the reverse is 65.chr
but I can find no documentation for this (aside from the above line)

And I wanted to find out more about it, so am trying things like:>> ?A
=> 65

hi = "Hello World"

=> "Hello World"

hi.each {|letter| puts ?letter }

SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
       from (irb):2

hi.each {|letter| puts ?"#{letter}" }

^C

hi

=> "Hello World"

hi.each {|letter| puts letter.? }

(irb):5: warning: invalid character syntax; use ?\s
SyntaxError: compile error
(irb):5: syntax error, unexpected '?'
hi.each {|letter| puts letter.? }
                              ^
       from (irb):5

hi.each {|letter| puts letter.?\s }

SyntaxError: compile error
(irb):6: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s }
                                ^
       from (irb):6

hi.each {|letter| puts letter.?\s65 }

SyntaxError: compile error
(irb):7: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s65 }
                                ^
       from (irb):7

--
use.inject do |as, often| as.you_can - without end

Hi,

···

On Sep 7, 2008, at 11:20 AM, Victor H. Goff III wrote:

I know that the reverse is 65.chr
but I can find no documentation for this (aside from the above line)

And I wanted to find out more about it, so am trying things like...

Look at the documentation for String#each, String#each_byte, and String#each_char.

(For String#each_char, make sure you require 'jcode' if you're running Ruby 1.8.6 or lower.)

Stephen

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.

TPR.

···

--
Posted via http://www.ruby-forum.com/\.

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.

TPR.
--
Posted via http://www.ruby-forum.com/\.