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

  • Thread starter Victor H. Goff III
  • Start date
V

Victor H. Goff III

[Note: parts of this message were removed to make it a legal post.]

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
=> 65SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
from (irb):2(irb):5: warning: invalid character syntax; use ?\s
SyntaxError: compile error
(irb):5: syntax error, unexpected '?'
hi.each {|letter| puts letter.? }
^
from (irb):5SyntaxError: compile error
(irb):6: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s }
^
from (irb):6SyntaxError: compile error
(irb):7: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s65 }
^
from (irb):7
 
R

Robert Klemme

2008/9/7 Victor H. Goff III said:
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
SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
from (irb):2
(irb):5: warning: invalid character syntax; use ?\s
SyntaxError: compile error
(irb):5: syntax error, unexpected '?'
hi.each {|letter| puts letter.? }
^
from (irb):5
SyntaxError: compile error
(irb):6: syntax error, unexpected tINTEGER
hi.each {|letter| puts letter.?\s }
^
from (irb):6
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
 
S

Stephen Celis

Hi,

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
 
T

Thomas B.

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.
 
V

Victor H. Goff III

[Note: parts of this message were removed to make it a legal post.]

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.





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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top