Forum: Ruby VERY simple question about "?"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-04 12:59
(Received via mailing list)
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-01-04 13:36
(Received via mailing list)
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
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2009-01-04 14:15
(Received via mailing list)
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/lang...
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-04 14:51
(Received via mailing list)
Yaser Sulaiman wrote:
>
> Regards,
> Yaser
>
> [1]: http://whytheluckystiff.net/ruby/pickaxe/html/lang...
>
>
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-04 15:50
(Received via mailing list)
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.

> 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
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (mguterl)
on 2009-01-04 16:33
(Received via mailing list)
On Sun, Jan 4, 2009 at 9:48 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>> see the equivalence between a string delimiter, or a character that signals
>
> irb(main):005:0> a = Object.new
> Basically "?a" is just another textual representation for the integer 97
> 1073491880
>
> 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/programming-ruby

There definitely is an option to select a PDF version for purchase.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-04 17:14
(Received via mailing list)
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/programming-ruby
>
> 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
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 00:08
(Received via mailing list)
Robert Klemme wrote:
>>>
>
> 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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2009-01-05 00:36
(Received via mailing list)
On Sun, Jan 4, 2009 at 5:06 PM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>>>>
>>> There definitely is an option to select a PDF version for purchase.
>
> languages.
> time.
Not sure if the following list is comprehensive, but useful
nonetheless...

http://www.zenspider.com/Languages/Ruby/QuickRef.html

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

Todd
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2009-01-05 00:39
(Received via mailing list)
On Sun, Jan 4, 2009 at 5:36 PM, Todd Benson <caduceass@gmail.com> wrote:
>>>
>>>>
>>>
>> - in the sense that I'm accustomed to the use of the word in programming
>> 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/QuickRef.html
>
> ...you'll find the first use of ? under Reserved words.

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

Todd
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2009-01-05 01:02
(Received via mailing list)
On Jan 4, 2009, at 5:06 PM, Tom Cloyd wrote:

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

And we appreciate that :)

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

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

?<char>, tho' is not an operator, any more than the quote is in "cat"
or the slash in /cat/. In all three cases they're simply syntax for
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
> 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.

Let me know what to add, and I'll definitely consider adding it. But
be aware that I don't really consider the concept of "an operator" to
be particular primary in the description of the language, because in
Ruby there'll always be debate about just what _is_ an operator. The
precedence table is my best take on it—I derived it from the parser,
and I believe it contains every operator-like thing I could find in
there. But, as I said, I'd love to hear suggestions, as the third
edition is nearing completion, and I'm always open for ideas to make
it better.


Cheers


Dave
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 02:23
(Received via mailing list)
Todd Benson wrote:
>>>>> <shortcutter@googlemail.com> wrote:
>>>>>>
>>>> robert
>>>
>>> To me, this is simply strange. Dave Thomas, whose contributions to the Ruby
>
> I meant beneath Reserved words.  It's actually classified under Types/Numbers.
>
> Todd
>
>
>
Definitely a useful page, about which I've known for some time.
Outstanding for its brevity. Limited at times, because if you don't
already know what he's talking about, you're not much enlightened by the
page content at all. Two examples:
* "Here docs" (right above "Symbols"). I know what this is, and I STILL
cannot quickly make sense of the notes on the pages about this
 functionality.
* Operators - has the same problem as Thomas' book: precedence is shown,
but basic operator function appears to be assumed to be known. I'm
sorry, but I wasn't born with that knowledge. Look at this line: <=> ==
=== != =~ !~

There's some very strange stuff there. I'm only certain about the
function of ONE of these, and I've been using Ruby for probably over a
year an a half (remember - I'm an intermittent weekend programmer, of
necessity).

All that said, I find Ryan Davis's work simply amazing, and I'm very
grateful for his presence in the world. His "Ruby QuickRef" at
http://www.zenspider.com/Languages/Ruby/QuickRef.html#3 is likely
supposed to be more cheat sheet than brief tutorial, to be fair.

The best presentation of Ruby operators I've yet found (looking mostly
on the web) is at http://www.tutorialspoint.com/ruby/ruby_operators.htm.
Also good are those at
http://www.techotopia.com/index.php/Ruby_Operators and
http://www.devarticles.com/c/a/Ruby-on-Rails/Ruby-....
So, this stuff can be found, but you have to go looking for it.

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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 03:15
(Received via mailing list)
Dave Thomas wrote:
>> 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.
>
> Which methods/operators are missing from the precedence table? I'll
> add them.
Oh, man...now I've going to have to get rigorous in my consideration of
the topic, aren't I? Heh heh. Probably about time, too. So...I'll go on
a search and recover mission, if only for my own edification. If I find
anything, I'll report back. Probably will anyway.

But to me the basic problem with the p. 324 table, as I suggested, is
that its subject is about precedence - a completely legitimate subject -
and is NOT primarily a presentation of operators with an explication of
what they DO. THAT's what I looked for practically the first time I
encountered the book, and have been looking for ever since.

As I just said in a response to Todd's post, The best presentation of
Ruby operators I've yet found (looking mostly on the web) is at
http://www.tutorialspoint.com/ruby/ruby_operators.htm. THIS is the sort
of presentation I expect in ANY language book. It seems fully as basic
as a definition of what a variable is and how one works, along with all
the other basic building blocks of the language. Languages are build up
from primitives - analogous to the premises in a logical argument. If
the premises are unclear, the argument cannot go forward, and that's
been exactly my experience with Ruby. I encounter some strange operator
- like the "?" encounter I had last night, and I want to go read about
this thing. After well over 30 minutes search through quite a range of
material, I had found next to nothing. That's nuts, to me.

>
> ?<char>, tho' is not an operator, any more than the quote is in "cat"
> or the slash in /cat/. In all three cases they're simply syntax for
> literals.
I'm still struggling here. When syntax is productive of something, I see
an operation occurring - active functionality. I'm perfectly content to
see operators as methods. Syntactically there's no problem that I can
detect in that construct. But to call "?<char>" a literal...well, I must
go study up a bit on this. To me, "abcdef" is a literal. '\n' is too.
"\n" is a literal if you allow meaning to be contexturalized, which
should be no problem. As input to various processes, '\n' and "\n" have
different outcomes, but by themselves produce nothing. "?<char>"
produces something. Irb makes this clear (to me):

irb(main):006:0> puts '\n'
\n
=> nil
irb(main):007:0> puts "\n"

=> nil
irb(main):008:0> puts ?\n
10
=> nil
irb(main):009:0>

If in "-1", we consider the "-" to be an operator, then I see an exact
parallel to the function of "?a".

It's not the same as what quote marks do - THEY provide context, and
change meaning. Operators PRODUCE something. They "operate". It seems
very simple. If I'm making a fundamental error, I'd be very grateful to
have it point out to me. I highly value arriving at a place of clear
understanding, when it's possible.
>> this for a long time.
>
> Let me know what to add, and I'll definitely consider adding it. But
> be aware that I don't really consider the concept of "an operator" to
> be particular primary in the description of the language, because in
> Ruby there'll always be debate about just what _is_ an operator. The
> precedence table is my best take on it—I derived it from the parser,
> and I believe it contains every operator-like thing I could find in
> there. But, as I said, I'd love to hear suggestions, as the third
> edition is nearing completion, and I'm always open for ideas to make
> it better.
Dave, the function descriptions in this table are too minimal, I think.
Not enough for a beginner - not for this one, anyway, and Ruby is at
least the fifth language I've dug around in. In all the others (Fortran,
APL, Pascal/Delphi, Basic, Python, a tabular presentation of the
operators the languages recognizes was shown to me EARLY, and I always
found it highly informative, since it told me what I could DO with
primitive units of data. Seems like an excellent starting point for more
complex topics.

Your comment about the operator concept is very interesting. I think
that it would be really, truly helpful for you to lay out Ruby more from
the ground up - shown me clearly it fundamental components, beginning
with the 'primitives' - reserved words are primitives, and so are
variables, etc. Operators, too, I'd say. From these things we build
other things, like data containers - meta-primitives, if you will.

I have always been greatly bewildered that in your book (I'm looking at
2nd edition PDF again), we're into arrays and hashes on p. 14 and I have
to encounter anything substantive about operators - those things that
glue together variables and constants so that we can form expressions.

Maybe I just bring the wrong mindset to Ruby. I'm willing to be told
that, if I'm shown a better one. However, I'd bet that a great many of
us did study grammar in school (remember "parts of speech"?), and some
of us have studied linguistics, and many of us have at least dabbled in
other languages. There are conventions about how we talk about human
communication, in general, computer languages included. When I come to a
presentation of a new language, the first thing I look for is the
familiar. I want some secure ground I can readily claim ("oh, I know
what THAT is!"), so that I can move from that out into the wilderness of
new territory.

For me that secure ground in anything of linguistic nature will always
be the "meme" concept - in formal linguistics this would be an analysis
of phonemes (aural primitives - irreducible units of sound) and
graphemes (irreducible units of  sound representation), etc. None of
these concepts are perfect, but they ARE plainly very useful. The basic
model is very familiar: premises (or data, if you will) + logic (legal
operations) = argument (or assertion or expression). This model is a
part of our cultural memory, as it were.

I want someone to show me the pieces of something, and how they go
together. I don't want complexity until I grasp the little things, at
least at the conceptual, if not the operational/functional, level. I
love the highly informative narratives you lay out in your book, about
all manner of topics. But I'm bewildered that in Ch. 2, the first real
content about the language per se, we're right away talking about OO
programming. I don't yet have the pieces of the language, at this point.
How can I think about how OO programming is done in Ruby. It's as if I'm
being asked to walk on air. Obviously, this is just one man's reaction,
and I full well know you'll see it that way. No problem.

If ANY of this is of any use to you in any way, it's worth the time it
took for me to lay it out. I don't expect a response. I'm only hoping
that maybe some of my perspective might be useful to you. If you get a
chance to read it at all, I'm content simply with that.

3rd edition coming out soon, eh? I have to say that one of the great
joys of being in the Ruby community is that the future looks so
interesting - and this is one piece of it we can all look forward to.
I'm sure you'll have my money soon enough, and it'll be a bargain! Best
of luck with your work on this project, and my thanks as well.

Thanks...

Tom

>
>
> Cheers
>
>
> Dave
>


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 03:40
(Received via mailing list)
Robert Klemme wrote:
>> 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?
Meaning, in linguistics as I understand it, is inherent, contextural, or
both. "a" is inherently a character in the basic Roman alphabet used by
many European languages. As a representation of a sound, its meaning
changes depending upon context. Consider "pat" and "pate". Adding the
suffix "e" to the consonant+vowel+consonant construction changes the
sound of the "a". In Ruby, customer_count (a variable) and
"customer_count" (a string) don't mean the same thing, because the
latter has a prefix and suffix of the " symbol. Contextural meaning
production is occurring. Ditto for the use of ":" in symbols. The """
and the ":" are context indicators, but produce nothing. Not so for "+"
in "a+b" or, I think "?a". Something is emitted, here. That goes well
beyond mere contextual meaning production, which is also occurring, to
be sure. I hope I'm being clear enough here.
>
> 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>
Thanks! That's fun, and interesting.
>
>> 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 }
A "textural representation"  that PRODUCES something? Doesn't make sense
to me. Representations simply represent. They "stand for" something
else, and do no more.

Now, I can define method X, and then say that X "stands for" the method
specification. That's true, from the compiler's point of view, among
other things. But to say no more is misleading. "X" can be called, at
which point it ACTS. "Tom" - my name can also be called, but when one
does so IT doesn't spring into action at all. I might, but IT cannot.

There is, I therefore assert, a fundamental difference here, and I think
you're seeing that two different things have the same feature and
concluding from that that they are the same sort of thing. That argument
is obviously flawed. "It's incomplete, as my "Tom" demonstrations shows.
You say "Basically "?a" is just another textual representation for..." -
I'd agree, except for that "just". That single word makes the statement
incorrect, to my mind, and also reveals the problem I'm addressing.
> foo
> 1073491940
> foo
> 1073492040
> foo
> 1073491880
> => 3
> irb(main):004:0>
>
[...]

Thanks for your thoughts. I appreciate your sharing them.

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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2009-01-05 04:28
(Received via mailing list)
On Jan 4, 2009, at 8:14 PM, Tom Cloyd wrote:

>
>
> If in "-1", we consider the "-" to be an operator, then I see an
> exact parallel to the function of "?a".
>
> It's not the same as what quote marks do - THEY provide context, and
> change meaning. Operators PRODUCE something. They "operate". It
> seems very simple. If I'm making a fundamental error, I'd be very
> grateful to have it point out to me. I highly value arriving at a
> place of clear understanding, when it's possible

I think you're over thinking it. For example, in

   0x10

which is 16 in decimal, is the '0x' part an operator? If you say no
(which I hope you do) then why would you say that ? is an operator in ?
a (which is 97). They are simply characters that are used by the
lexical analysis phase to help interpret what follows. No executable
code is generated: there's no operation.

Similarly, in your example of -1, the - sign is NOT an operator. It
simply means that when parsing the constant, a negative value is
required. Again, so executable code is generated. That's the
difference between

    a = -1      # -1 is a constant, no operator
    b = -a      # the unary minus operator (-@) is invoked on the
object referenced by a

Cheers


Dave
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 07:44
(Received via mailing list)
Dave Thomas wrote:
>> processes, '\n' and "\n" have different outcomes, but by themselves
>> 10
>> clear understanding, when it's possible
>
>
>
> Dave
>
>
>
>
Thanks, Dave. This helps. You have a genuine knack for simple clear
statement and example. Thanks for the dialog!

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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-05 10:58
Tom Cloyd wrote:
> * Operators - has the same problem as Thomas' book: precedence is shown,
> but basic operator function appears to be assumed to be known. I'm
> sorry, but I wasn't born with that knowledge. Look at this line: <=> ==
> === != =~ !~

There aren't really any operators in Ruby. Ruby simply maps all these to
method calls (*), so to find out what the operator *means* you have to
look at the class documentation for that method.

e.g.
  str1 = "foo"
  str2 = "bar"
  p str1 <=> str2

Here you need to look at the documentation for String#<=> to see what it
means. Yes, "<=>" is a legitimate method name :-) You can also call it
like this:

  str1.<=>(str2)

The only real purpose of the operator precedence table is to show how
expressions containing multiple operators are resolved. e.g.

   1 + 2 * 3

#  same as:
#  1 + (2 * 3)

#  same as:
#  1.+(2.*(3))

Note that even the methods '+' and '*' do not necessarily have anything
to do with addition or multiplication. It's entirely dependent on the
target object how it implements these methods. For example:

   "123" * 3    # => "123123123"

HTH,

Brian.

(*) The exceptions in ruby 1.8 are != and !~, which invoke the methods
== and =~ but then negate the result
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-05 13:03
(Received via mailing list)
Brian Candler wrote:
> look at the class documentation for that method.
>   str1.<=>(str2)
> #  1.+(2.*(3))
>
> (*) The exceptions in ruby 1.8 are != and !~, which invoke the methods
> == and =~ but then negate the result
>
Ah...This is actually very helpful. I think we have a language problem,
though. If there are no operators in Ruby, we should call all those
methods with non-alphabetical names something besides "operators". I
know...we get USED to nonsense. Every try to learn to spell English? I
still don't do it well. But we live with it. Here, I think we need to
change not only the computer language (experts appear to be evolving it
quite nicely, and I'm happy just to watch), but the human language we
use to talk about it. I guess this particularly matters to me because I
write a lot - daily. I virtually live at a keyboard. Verbal coherence
matters a lot to me, even if I can't always achieve it.

OK, let's assume we have the language problem solved (ha! - not likely
to happen). Nann-methods == "non-alphabetically-named methods". The core
problem still remains - what the blazes does that THING
do/mean/produce...??? Here's were your post is helpful to me. I will
follow your suggestion. I feel better already, because for the first
time in a long time [a] I understand my confusion, AND [2] what to do
about it. A whole new world.

Dave...if you're still listening...it'd be lovely if some clarification
along the lines of this post made it into your 3rd edition - that's what
I find myself thinking, anyway.

Thanks to all...for hanging with me on this one.

Thanks.

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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-05 13:32
(Received via mailing list)
Tom Cloyd wrote:
> <=> == === != =~ !~
>
> There's some very strange stuff there. I'm only certain about the
> function of ONE of these

As has already been mentionen, they are methods and do whatever they're
defined to do. That being said here's what their intended function is:
<=> should be defined by any class that includes the Comaparable module.
It
should return -1,0 or 1 depending on whether the receiver (the lhs) is
less
than, equal to or greater than the argument (the rhs).

== should return true if (and only if) the lhs is equal to the rhs
(where the
definition of equality depends on the class). Object#== is defined to
return
true if two objects are actually the same object (i.e. both the receiver
and
the argument are actually references to the same location in memory).

=== defines when an object "matches" another object in a
case-when-statement
(or the grep method). For example in the following code, the puts will
be
executed if y === x is true:
case x
when y
puts "tada"
end
Object#=== is defined to return the same as ==, Regexp#=== is defined to
return true if the regexp matches a string and Range#=== is defined to
return true if a given object is inside the range.

!= is simply the negation of ==, so x != y is equivalent to !(x == y)

Regexp#=~, which takes a string as an argument, and String#=~, which
takes a
regex as an argument, return the index where the regex matches the
string or
nil if the regex does not match the string.

All of those are documented in the API docs btw.

HTH,
Sebastian
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-05 13:35
Tom Cloyd wrote:
> OK, let's assume we have the language problem solved (ha! - not likely
> to happen). Nann-methods == "non-alphabetically-named methods".

That's not exactly the same though. You can (with a bit of work) define
arbitrary methods with non-alphanumeric names, but they are not
operators.

The things we're talking about are called "operators" because they look,
syntactically, like operators in other languages, and are parsed as
such.

     foo + bar          # infix binary plus (method +)
     - foo              # prefix unary minus (method -@)

That is:  "arg1 op arg2"  instead of  "arg1.methodname(arg2)"

The fundamental thing is that the infix/prefix notation shown above is
parsed successfully, but then translated into normal method calls. You
can then define or redefine these methods, just like any other methods.

class Foo
  def -@
    "minus #{self}"
  end
end

f = Foo.new
p -f        # operator notation (unary minus)
p f.-@      # same but using explicit method call

> The core
> problem still remains - what the blazes does that THING
> do/mean/produce...???

It produces whatever it is defined to produce :-)

That is, class Foo may or may not implement the unary minus
operator/method (or any other operator). If it does, it is the
programmer's choice as to what it does.

Equally, I could define a method Foo#xcikuhruh. It would be my choice
what it does :-)

You may have an implicit expectation of, say, what the "*" operator
does, i.e. multiplication. But it may not make sense in non-numeric
contexts. For example, the facets library defines a way to combine Proc
objects using *:

----------------------------------------------------------------- Proc#*
     *(x)
------------------------------------------------------------------------
     Operator for Proc#compose and Integer#times_collect/of.

       a = lambda { |x| x + 4 }
       b = lambda { |y| y / 2 }

       (a * b).call(4)  #=> 6
       (b * a).call(4)  #=> 4

       CREDIT: Dave

An operator precedence table shows all the operators known by Ruby, and
the ways they interact with each other in complex expressions. It
doesn't make sense to include what they "mean", because they will mean
different things when applied to different objects. And the joy is, you
can make them mean whatever you like when applied to *your* objects.

A neat trick I've seen is a pathname class which uses the '/' operator
to compose paths: e.g.

  path1 = MyPath.new("home")
  path2 = path1 / "bar" / "baz"     # results in /home/bar/baz

Saying that the '/' operator means "division" doesn't make sense here.

HTH,

Brian.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-05 18:23
(Received via mailing list)
On 05.01.2009 10:58, Brian Candler wrote:
> There aren't really any operators in Ruby. Ruby simply maps all these to
> method calls (*), so to find out what the operator *means* you have to
> look at the class documentation for that method.

> The only real purpose of the operator precedence table is to show how
> expressions containing multiple operators are resolved. e.g.

Brian, you are contradicting yourself: first you deny the presence of
operators in Ruby and then you talk about them nevertheless. :-)

Fact is, there _are_ operators in Ruby - and they do have a precedence.
   (I am not sure why you put an "only" into the sentence above -
operator precedence is what a precedence table is about.)

Fact is also, that their semantics are defined via methods.  Note also
that for understanding the semantics of most binary operators the method
#coerce plays a crucial role (=> double dispatch).

Kind regards

  robert
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-05 18:48
(Received via mailing list)
On 05.01.2009 03:38, Tom Cloyd wrote:
>>> 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?
> Meaning, in linguistics as I understand it,

Unfortunately we're in the domain of programming languages which is
quite different from linguistics.  Part of my difficulty understanding
you is probably caused by the fact that you seem to try to tackle
problems of computer languages with tools (terms and theories) from a
complete different domain.  While both domains happen to share terms I
think it is not helpful to try to apply linguistics (i.e. the science of
natural languages) to computer languages.  It may actually hinder
understanding.

> Ditto for the use of ":" in symbols. The """
> and the ":" are context indicators, but produce nothing. Not so for "+"
> in "a+b" or, I think "?a".

Actually I believe that "?a" does not belong in the same category as
"a+b" because, as Dave and I have pointed out already, "?a" is just
another way to write the integer 97 ("97", "0141" and "0x61" are other
ways to write 97).

>>> 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:
>>
> A "textural representation"  that PRODUCES something?

I did not mention "produce" in the context of - where do you take that
from?

> Doesn't make sense
> to me. Representations simply represent. They "stand for" something
> else, and do no more.

Yes, all these character sequences ("?a", "97", "0141" and "0x61")
represent the Fixnum instance which in term is a technical
representation (i.e. data) of the (mathematical) number 97.  If an
expression of a Ruby program is evaluate that contains any of these
tokens, the token (which is an expression as well, more precisely a
constant expression) evaluates to the said Fixnum instance.

> Now, I can define method X, and then say that X "stands for" the method
> specification. That's true, from the compiler's point of view, among
> other things. But to say no more is misleading. "X" can be called, at
> which point it ACTS. "Tom" - my name can also be called, but when one
> does so IT doesn't spring into action at all. I might, but IT cannot.

It does, as my example demonstrated.  Opposed to "?a" "'Tom'" is not a
constant expression but a String constructor.  (See my example below
which I took the liberty to put together again.)

> There is, I therefore assert, a fundamental difference here, and I think
> you're seeing that two different things have the same feature and

Actually I pointed out that "?a" and "'Tom'" are not the same sort of
thing.  (On a certain level they are of course: both are valid syntax
constructs (tokens) of the Ruby language.  Actually, both are even
expressions which happen do have different semantics - constant vs. non
constant).

> concluding from that that they are the same sort of thing.

Again, I do not know where you take that from.  This is certainly
nothing I have intended to convey.

> That argument
> is obviously flawed. "It's incomplete, as my "Tom" demonstrations shows.
> You say "Basically "?a" is just another textual representation for..." -
> I'd agree, except for that "just". That single word makes the statement
> incorrect, to my mind, and also reveals the problem I'm addressing.

Not at all.  There are many ways to write down 97 in a Ruby program (as
I have shown above).  In that sense "?a" is just another way to write it
down (assuming "97" is considered the most commonly used way).

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

Cheers

  robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-05 23:21
Robert Klemme wrote:
> Brian, you are contradicting yourself: first you deny the presence of
> operators in Ruby and then you talk about them nevertheless. :-)
>
> Fact is, there _are_ operators in Ruby - and they do have a precedence.
>    (I am not sure why you put an "only" into the sentence above -
> operator precedence is what a precedence table is about.)

Operators in Ruby are nothing more than syntactic sugar for method
calls.

The "only" was because the OP wanted the table to include the "meaning"
of each operator. I am agreeing with you that an operator precedence
table should show only operator precedence :-)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-06 12:15
(Received via mailing list)
On 05.01.2009 23:21, Brian Candler wrote:
> Robert Klemme wrote:
>> Brian, you are contradicting yourself: first you deny the presence of
>> operators in Ruby and then you talk about them nevertheless. :-)
>>
>> Fact is, there _are_ operators in Ruby - and they do have a precedence.
>>    (I am not sure why you put an "only" into the sentence above -
>> operator precedence is what a precedence table is about.)
>
> Operators in Ruby are nothing more than syntactic sugar for method
> calls.

Well, in a way that's what they are in every procedural language - they
are just a special notation for a method / function invocation, aren't
they?  Even if technically a compiler does not generate a subroutine
call, they represent the code inserted there which is always the same
for the same types of arguments.  What makes operators special in some
procedural languages is that they are overloaded (plus for int, plus for
float etc.) while functions cannot be overloaded in those languages.
And, of course, they need a defined precedence because of the different
syntax (i.e. their arguments are not grouped via brackets as for
function / method calls).

> The "only" was because the OP wanted the table to include the "meaning"
> of each operator. I am agreeing with you that an operator precedence
> table should show only operator precedence :-)

:-) Ah, I see.  In case of Ruby the meaning cannot be included anyway
because operators are overloaded and can be overloaded by the user - so
there is no fixed meaning.

Kind regards

  robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-06 12:41
Robert Klemme wrote:
>> Operators in Ruby are nothing more than syntactic sugar for method
>> calls.
>
> Well, in a way that's what they are in every procedural language - they
> are just a special notation for a method / function invocation, aren't
> they?

I don't think so. Well, not in C anyway. The operation "a + b" is
defined to be an operation on a and b (e.g. integer addition or float
addition). It is not defined as a function call, and it cannot be
changed by the user.

The same applies to Perl I think.

In Ruby, + is not defined in terms of its operation (i.e. what it does
with the arguments), but just that it dispatches to a method on the
left-hand argument. They really are syntactic sugar, since they parse to
exactly the same as the explicit method call syntax:

$ cat add1.rb
1 + 2 * 3
$ cat add2.rb
1.+(2.*(3))
$ parse_tree_show add1.rb
s(:call,
 s(:lit, 1),
 :+,
 s(:array, s(:call, s(:lit, 2), :*, s(:array, s(:lit, 3)))))
$ parse_tree_show add2.rb
s(:call,
 s(:lit, 1),
 :+,
 s(:array, s(:call, s(:lit, 2), :*, s(:array, s(:lit, 3)))))

Now, in principle a C compiler could be written such that a+b is
rewritten at the parsing stage to cc_int_add(a,b) or somesuch. But this
wouldn't be a function like a user-written one. For a start, it would
have to generate machine code or some intermediate VM code directly,
since there is no primitive '+' operator to use. Secondly, it would need
to be able to handle arguments in different combinations of registers,
so that an expression like cc_int_add(a, cc_int_mul(b,c)) doesn't spill
registers everywhere.

So cc_int_add() might *look* like a function, but actually it would be a
special node type which generates addition code. In that case, the parse
tree may as well remember that it's a "+" operator with two arguments,
since converting it to something which looks like a function call (but
isn't) doesn't help at all.

Regards,

Brian.
This topic is locked and can not be replied to.