What is '?' in Ruby?

  • Thread starter Alexander Bubnov
  • Start date
A

Alexander Bubnov

Hello!
To clarify the question I need to say some more.
I know
Ruby 1.8 '?a' => 97
Ruby 1.9 '?a' => "a"
Really I would like to where I can get specification of operator,
keyword, function or something else I do know what is name of '?'...
I have surfed ruby-lang.org and ruby-doc.org but can't find information
about it. Is it an unofficial something?
Just a bit here http://www.ruby-doc.org/docs/ProgrammingRuby/, "Standard
Types" chapter:
?a # character code

But it is related to 1.8 version, not for 1.9.
Please just point to chapter of ruby specification where I can find
anything about '?' if it is. And are there methods which helps to know
what is '?'?

Thank you in advance.
 
B

Brian Candler

Alexander said:
I know
Ruby 1.8 '?a' => 97
Ruby 1.9 '?a' => "a"

It's a character literal - but in 1.9 a character is a one-character
string (possibly multiple bytes) rather than an Integer for a single
byte in 1.8.

You'll see the same difference with "a"[0]
Really I would like to where I can get specification of operator,
keyword, function or something else I do know what is name of '?'...

For 1.8 (actually 1.6.8 :)

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html

"You can also get the integer value corresponding to an ASCII character
or escape sequence by preceding it with a question mark"

For 1.9: you have to buy the more recent version of this book.
Please just point to chapter of ruby specification

Please point me to any ruby specification :)

Actually, there is a project which is attempting to create a
specification of Ruby for standardisation; it was linked to in this
mailing list within the last few days. However it's using 1.8.7 as it's
starting point, on the grounds that 1.9 is still a moving target.

I attempted to document strings in 1.9, but only got as far as
documenting about 200 behaviours.
http://github.com/candlerb/string19/blob/master/string19.rb
 
F

Florian Gilcher

Hello!
To clarify the question I need to say some more.
I know
Ruby 1.8 '?a' =3D> 97
Ruby 1.9 '?a' =3D> "a"
Really I would like to where I can get specification of operator,
keyword, function or something else I do know what is name of '?'...
I have surfed ruby-lang.org and ruby-doc.org but can't find = information
about it. Is it an unofficial something?
Just a bit here http://www.ruby-doc.org/docs/ProgrammingRuby/, = "Standard
Types" chapter:
?a # character code
=20
But it is related to 1.8 version, not for 1.9.
Please just point to chapter of ruby specification where I can find
anything about '?' if it is. And are there methods which helps to know
what is '?'?

Ruby 1.8 sees strings as a sequence of bytes. So

"a"[0] #=3D> 97

yields 97 (because 97 is the value representing the character a in an =
ASCII-String). If you want to compare this to some character, you need =
the character code of "a" (because no one wants to have an ASCII-Table =
next to them when programming). ?a gives you the character code of a. So
=20
"a"[0] =3D=3D ?a #=3D> true
"a"[0] =3D=3D "a" #=3D> false

Now, things have changed in the String world in Ruby 1.9. Strings now =
know their encoding and return charactes instead of codepoints when =
accessing single characters.

"a"[0] #=3D> "a"

To make sure that legacy applications do not break, ?a will now return =
"a", so now the behaviour is as follows:

"a"[0] =3D=3D ?a #=3D> true
"a"[0] =3D=3D "a" #=3D> true

Regards,
Florian Gilcher=
 
A

Alexander Bubnov

Florian said:
On Mar 1, 2010, at 1:31 PM, Alexander Bubnov wrote:


Ruby 1.8 sees strings as a sequence of bytes. So

"a"[0] #=> 97

yields 97 (because 97 is the value representing the character a in an
ASCII-String). If you want to compare this to some character, you need
the character code of "a" (because no one wants to have an ASCII-Table
next to them when programming). ?a gives you the character code of a. So

"a"[0] == ?a #=> true
"a"[0] == "a" #=> false

Now, things have changed in the String world in Ruby 1.9. Strings now
know their encoding and return charactes instead of codepoints when
accessing single characters.

"a"[0] #=> "a"

To make sure that legacy applications do not break, ?a will now return
"a", so now the behaviour is as follows:

"a"[0] == ?a #=> true
"a"[0] == "a" #=> true

Regards,
Florian Gilcher

Thank you! Have you ever see something about '?' in specification of
ruby1.9? If so can you please point to that doc. Unfortunately I cannot
find anything about it but I would like to know it because, for example,
in ruby1.10 it can be changed in once day.
 
A

Alexander Bubnov

Thank you! Have you ever see something about '?' in specification of
ruby1.9? If so can you please point to that doc. Unfortunately I cannot
find anything about it but I would like to know it because, for example,
in ruby1.10 it can be changed in once day.

Many thanks to all!
I am sorry I did not see post about documentation!
Now it is clear!
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top