[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

VERY simple question about "?"

Tom Cloyd

1/4/2009 11:58:00 AM

I absolutely love Ruby, but...I've always found the subject of Ruby
operators to be a small nightmare. First, it's hard to find a complete
table - the best I've found says gleefully at its bottom "and there are
even more". Wonderful. So many tools, so little time...

Then there's the matter of some symbols being operators, others being
methods, and some (my impression only) both, and yet others being a
number of things, depending upon context. When someone says that Ruby is
harder than Python, I suppose this might be what they mean. makes sense
to me.

So, just today I learn that this is possible:

irb(main):001:0> ?.
=> 46
irb(main):002:0> ?a
=> 97
irb(main):003:0> 97.chr
=> "a"
irb(main):004:0>

So...what is this "?" thing? I've looked in four references, and Googled
until I'm dizzy, and find no answer. Operator? Method? (Stupid
distinction???)

I know I'll certainly be using this, but it'd be nice to know what I'm
doing.

All comments welcome.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


42 Answers

F. Senault

1/4/2009 12:34:00 PM

0

Le 4 janvier 2009 à 12:58, Tom Cloyd a écrit :

> So...what is this "?" thing? I've looked in four references, and Googled
> until I'm dizzy, and find no answer. Operator? Method? (Stupid
> distinction???)

It's simply a language construct, like the ' that delimitates a string
or the : that starts a symbol.

If you have the pickaxe book, it's documented in the "Integer and
Floating-Point Numbers" section (page 304 and 305 of my PDF edition).

Fred
--
Go not to Usenet for counsel, for they will say both 'No' and 'Yes' and
'Try another newsgroup'.

Yaser Sulaiman

1/4/2009 1:14:00 PM

0

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

On Sun, Jan 4, 2009 at 3:34 PM, F. Senault <fred@lacave.net> wrote:

> If you have the pickaxe book, it's documented in the "Integer and
> Floating-Point Numbers" section (page 304 and 305 of my PDF edition).
>

And if you don't have the book, you can read that section[1] in the online
version.

Regards,
Yaser

[1]: http://whytheluckystiff.net/ruby/pickaxe/html/langua...

Tom Cloyd

1/4/2009 1:49:00 PM

0

Yaser Sulaiman wrote:
> On Sun, Jan 4, 2009 at 3:34 PM, F. Senault <fred@lacave.net> wrote:
>
>
>> If you have the pickaxe book, it's documented in the "Integer and
>> Floating-Point Numbers" section (page 304 and 305 of my PDF edition).
>>
>>
>
> And if you don't have the book, you can read that section[1] in the online
> version.
>
> Regards,
> Yaser
>
> [1]: http://whytheluckystiff.net/ruby/pickaxe/html/langua...
>
>
Thanks, guys.

I guess what confused me is that I bring to Ruby the notion that if a
symbol causes something to happen, as "?" clearly does in my example (it
produces the integer code for a character), its' an operator or method.
I'm not unfamiliar with linguistics. I don't see the equivalence between
a string delimiter, or a character that signals the beginning of a
symbol, and a symbol that is actually productive of something. Makes no
sense to me at all, in fact. Maybe I'm failing to grasp some
transformative concept which changes how things work in the Ruby world.

Seeing operators as methods makes some real sense, on the other hand.
"=" produces something out of what it's given. It doesn't just change
meaning, which is all that happens with quotes or ":". That's the
distinction I'm seeing, and it seems valid.

I'll keep thinking about it.

Quite apart from this is the strange matter of how difficult it is to
find a simple, complete table of Ruby operators. I've basically given up
on that.

Thanks for the Pickaxe reference - I found it on PDF p. 332 of my PDF
version of the 2nd ed. I'd never seen that page.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Robert Klemme

1/4/2009 2:47:00 PM

0

On 04.01.2009 14:48, Tom Cloyd wrote:
> I guess what confused me is that I bring to Ruby the notion that if a
> symbol causes something to happen, as "?" clearly does in my example (it
> produces the integer code for a character),

In this case nothing "happens" (see below). It is just another
syntactical way to express an integer constant.

> its' an operator or method.
> I'm not unfamiliar with linguistics. I don't see the equivalence between
> a string delimiter, or a character that signals the beginning of a
> symbol, and a symbol that is actually productive of something. Makes no
> sense to me at all, in fact. Maybe I'm failing to grasp some
> transformative concept which changes how things work in the Ruby world.
>
> Seeing operators as methods makes some real sense, on the other hand.
> "=" produces something out of what it's given. It doesn't just change
> meaning, which is all that happens with quotes or ":". That's the
> distinction I'm seeing, and it seems valid.

I am not sure what you mean by "change meaning". Can you elaborate?

Btw, note that there are two types of "=":

1. variable assignment as in "a = 123",

2. method call that mimics variable assignment as in "a.foo = 456".

Both evaluate always to the right hand side - but this is about the only
commonality. Semantics of type 1 is fixed while you are free to
implement type 2 in any way you like, e.g.

irb(main):005:0> a = Object.new
=> #<Object:0x7ff79980>
irb(main):006:0> def a.foo=(x) puts "bah!" end
=> nil
irb(main):007:0> a.foo = 456
bah!
=> 456
irb(main):008:0>

> I'll keep thinking about it.

Basically "?a" is just another textual representation for the integer 97
which happens to be the ASCII code of character "a". It differs from
string delimiters because the expression "foo" is actually a String
constructor - it will create a new String object whenever it is executed:

irb(main):003:0> 3.times { x = "foo"; puts x, x.object_id }
foo
1073491940
foo
1073492040
foo
1073491880
=> 3
irb(main):004:0>

> Quite apart from this is the strange matter of how difficult it is to
> find a simple, complete table of Ruby operators. I've basically given up
> on that.

You can find it in the Pickaxe - a good book to have anyway.

> Thanks for the Pickaxe reference - I found it on PDF p. 332 of my PDF
> version of the 2nd ed. I'd never seen that page.

As far as I know there is no legal electronic version of the second
edition so you better double check whether you are allowed to use this.
You could as well buy the book to somewhat compensate for this.

Kind regards

robert

Michael Guterl

1/4/2009 3:32:00 PM

0

On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 04.01.2009 14:48, Tom Cloyd wrote:
>>
>> I guess what confused me is that I bring to Ruby the notion that if a
>> symbol causes something to happen, as "?" clearly does in my example (it
>> produces the integer code for a character),
>
> In this case nothing "happens" (see below). It is just another syntactical
> way to express an integer constant.
>
>> its' an operator or method. I'm not unfamiliar with linguistics. I don't
>> see the equivalence between a string delimiter, or a character that signals
>> the beginning of a symbol, and a symbol that is actually productive of
>> something. Makes no sense to me at all, in fact. Maybe I'm failing to grasp
>> some transformative concept which changes how things work in the Ruby world.
>>
>> Seeing operators as methods makes some real sense, on the other hand. "="
>> produces something out of what it's given. It doesn't just change meaning,
>> which is all that happens with quotes or ":". That's the distinction I'm
>> seeing, and it seems valid.
>
> I am not sure what you mean by "change meaning". Can you elaborate?
>
> Btw, note that there are two types of "=":
>
> 1. variable assignment as in "a = 123",
>
> 2. method call that mimics variable assignment as in "a.foo = 456".
>
> Both evaluate always to the right hand side - but this is about the only
> commonality. Semantics of type 1 is fixed while you are free to implement
> type 2 in any way you like, e.g.
>
> irb(main):005:0> a = Object.new
> => #<Object:0x7ff79980>
> irb(main):006:0> def a.foo=(x) puts "bah!" end
> => nil
> irb(main):007:0> a.foo = 456
> bah!
> => 456
> irb(main):008:0>
>
>> I'll keep thinking about it.
>
> Basically "?a" is just another textual representation for the integer 97
> which happens to be the ASCII code of character "a". It differs from string
> delimiters because the expression "foo" is actually a String constructor -
> it will create a new String object whenever it is executed:
>
> irb(main):003:0> 3.times { x = "foo"; puts x, x.object_id }
> foo
> 1073491940
> foo
> 1073492040
> foo
> 1073491880
> => 3
> irb(main):004:0>
>
>> Quite apart from this is the strange matter of how difficult it is to find
>> a simple, complete table of Ruby operators. I've basically given up on that.
>
> You can find it in the Pickaxe - a good book to have anyway.
>
>> Thanks for the Pickaxe reference - I found it on PDF p. 332 of my PDF
>> version of the 2nd ed. I'd never seen that page.
>
> As far as I know there is no legal electronic version of the second edition
> so you better double check whether you are allowed to use this. You could
> as well buy the book to somewhat compensate for this.
>
http://www.pragprog.com/titles/ruby/progra...

There definitely is an option to select a PDF version for purchase.

Robert Klemme

1/4/2009 4:13:00 PM

0

2009/1/4 Michael Guterl <mguterl@gmail.com>:
> On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:

>> As far as I know there is no legal electronic version of the second edition
>> so you better double check whether you are allowed to use this. You could
>> as well buy the book to somewhat compensate for this.
>>
> http://www.pragprog.com/titles/ruby/progra...
>
> There definitely is an option to select a PDF version for purchase.

Thanks for the education! I wasn't aware of this. There was
definitively an illegal PDF version offered (even here) in the past.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Tom Cloyd

1/4/2009 11:07:00 PM

0

Robert Klemme wrote:
> 2009/1/4 Michael Guterl <mguterl@gmail.com>:
>
>> On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
>> <shortcutter@googlemail.com> wrote:
>>
>
>
>>> As far as I know there is no legal electronic version of the second edition
>>> so you better double check whether you are allowed to use this. You could
>>> as well buy the book to somewhat compensate for this.
>>>
>>>
>> http://www.pragprog.com/titles/ruby/progra...
>>
>> There definitely is an option to select a PDF version for purchase.
>>
>
> Thanks for the education! I wasn't aware of this. There was
> definitively an illegal PDF version offered (even here) in the past.
>
> Kind regards
>
> robert
>
>
I wouldn't engage with a illegitimate copy - isn't playing fair at all.
I completely believe in "intellectual property". My copy certainly WAS
purchased from the Pragmatic Programmers website. Marvelous book, of
course, but it does not, to my best knowledge, contain a complete list
of operators. I've done everything I can think of to find it. I
seriously doubt that it's there. The precedence table on 324 is about
precedence, and not a primary presentation of operators and their function.

More incredibly, "operator" is not even in the index as a primary index
term - in the sense that I'm accustomed to the use of the word in
programming languages.

Here's the entry that IS there:

Operator
as method call 82, 335
precedence 324

To me, this is simply strange. Dave Thomas, whose contributions to the
Ruby community are inestimably valuable, may see this thread, in which
case I hope he comments. I've been completely bewildered about this for
a long time.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Todd Benson

1/4/2009 11:35:00 PM

0

On Sun, Jan 4, 2009 at 5:06 PM, Tom Cloyd <tomcloyd@comcast.net> wrote:
> Robert Klemme wrote:
>>
>> 2009/1/4 Michael Guterl <mguterl@gmail.com>:
>>
>>>
>>> On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
>>> <shortcutter@googlemail.com> wrote:
>>>
>>
>>
>>>>
>>>> As far as I know there is no legal electronic version of the second
>>>> edition
>>>> so you better double check whether you are allowed to use this. You
>>>> could
>>>> as well buy the book to somewhat compensate for this.
>>>>
>>>>
>>>
>>> http://www.pragprog.com/titles/ruby/progra...
>>>
>>> There definitely is an option to select a PDF version for purchase.
>>>
>>
>> Thanks for the education! I wasn't aware of this. There was
>> definitively an illegal PDF version offered (even here) in the past.
>>
>> Kind regards
>>
>> robert
>>
>>
>
> I wouldn't engage with a illegitimate copy - isn't playing fair at all. I
> completely believe in "intellectual property". My copy certainly WAS
> purchased from the Pragmatic Programmers website. Marvelous book, of course,
> but it does not, to my best knowledge, contain a complete list of operators.
> I've done everything I can think of to find it. I seriously doubt that it's
> there. The precedence table on 324 is about precedence, and not a primary
> presentation of operators and their function.
>
> More incredibly, "operator" is not even in the index as a primary index term
> - in the sense that I'm accustomed to the use of the word in programming
> languages.
>
> Here's the entry that IS there:
>
> Operator
> as method call 82, 335
> precedence 324
>
> To me, this is simply strange. Dave Thomas, whose contributions to the Ruby
> community are inestimably valuable, may see this thread, in which case I
> hope he comments. I've been completely bewildered about this for a long
> time.

Not sure if the following list is comprehensive, but useful nonetheless...

http://www.zenspider.com/Languages/Ruby/Qui...

...you'll find the first use of ? under Reserved words.

Todd

Todd Benson

1/4/2009 11:38:00 PM

0

On Sun, Jan 4, 2009 at 5:36 PM, Todd Benson <caduceass@gmail.com> wrote:
> On Sun, Jan 4, 2009 at 5:06 PM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>> Robert Klemme wrote:
>>>
>>> 2009/1/4 Michael Guterl <mguterl@gmail.com>:
>>>
>>>>
>>>> On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
>>>> <shortcutter@googlemail.com> wrote:
>>>>
>>>
>>>
>>>>>
>>>>> As far as I know there is no legal electronic version of the second
>>>>> edition
>>>>> so you better double check whether you are allowed to use this. You
>>>>> could
>>>>> as well buy the book to somewhat compensate for this.
>>>>>
>>>>>
>>>>
>>>> http://www.pragprog.com/titles/ruby/progra...
>>>>
>>>> There definitely is an option to select a PDF version for purchase.
>>>>
>>>
>>> Thanks for the education! I wasn't aware of this. There was
>>> definitively an illegal PDF version offered (even here) in the past.
>>>
>>> Kind regards
>>>
>>> robert
>>>
>>>
>>
>> I wouldn't engage with a illegitimate copy - isn't playing fair at all. I
>> completely believe in "intellectual property". My copy certainly WAS
>> purchased from the Pragmatic Programmers website. Marvelous book, of course,
>> but it does not, to my best knowledge, contain a complete list of operators.
>> I've done everything I can think of to find it. I seriously doubt that it's
>> there. The precedence table on 324 is about precedence, and not a primary
>> presentation of operators and their function.
>>
>> More incredibly, "operator" is not even in the index as a primary index term
>> - in the sense that I'm accustomed to the use of the word in programming
>> languages.
>>
>> Here's the entry that IS there:
>>
>> Operator
>> as method call 82, 335
>> precedence 324
>>
>> To me, this is simply strange. Dave Thomas, whose contributions to the Ruby
>> community are inestimably valuable, may see this thread, in which case I
>> hope he comments. I've been completely bewildered about this for a long
>> time.
>
> Not sure if the following list is comprehensive, but useful nonetheless...
>
> http://www.zenspider.com/Languages/Ruby/Qui...
>
> ...you'll find the first use of ? under Reserved words.

I meant beneath Reserved words. It's actually classified under Types/Numbers.

Todd

Dave Thomas

1/5/2009 12:01:00 AM

0


On Jan 4, 2009, at 5:06 PM, Tom Cloyd wrote:

> I wouldn't engage with a illegitimate copy - isn't playing fair at =20
> all. I completely believe in "intellectual property". My copy =20
> certainly WAS purchased from the Pragmatic Programmers website.

And we appreciate that :)

> Marvelous book, of course, but it does not, to my best knowledge, =20
> contain a complete list of operators. I've done everything I can =20
> think of to find it. I seriously doubt that it's there. The =20
> precedence table on 324 is about precedence, and not a primary =20
> presentation of operators and their function.

Which methods/operators are missing from the precedence table? I'll =20
add them.

?<char>, tho' is not an operator, any more than the quote is in "cat" =20=

or the slash in /cat/. In all three cases they're simply syntax for =20
literals.

> Here's the entry that IS there:
>
> Operator
> as method call 82, 335
> precedence 324
>
> To me, this is simply strange. Dave Thomas, whose contributions to =20
> the Ruby community are inestimably valuable, may see this thread, in =20=

> which case I hope he comments. I've been completely bewildered about =20=

> this for a long time.

Let me know what to add, and I'll definitely consider adding it. But =20
be aware that I don't really consider the concept of "an operator" to =20=

be particular primary in the description of the language, because in =20
Ruby there'll always be debate about just what _is_ an operator. The =20
precedence table is my best take on it=97I derived it from the parser, =20=

and I believe it contains every operator-like thing I could find in =20
there. But, as I said, I'd love to hear suggestions, as the third =20
edition is nearing completion, and I'm always open for ideas to make =20
it better.


Cheers


Dave=