More alias problems

At a guess, I’d say the interpreter knows that “operators” have a magic
syntax (i.e. send the appropriate method to the preceding object). This
magic happens whenever you use an operator, even when you define it for
your own classes.

However, coming across “2 plus 5” the interpreter has no idea what
‘plus’ is. It is not an operator (one of the symbols +, -, * etc.) so it
is not treated as such. It can not guess that it’s a method defined on
the preceding object (it expects the syntax for that to be o.meth).

I am not familiar with the exact internal workings of aliases. But look
at:

irb(main):005:0> class A
irb(main):006:1> def foo; end
irb(main):007:1> end
=> nil
irb(main):011:0> a = A.new
=> #<A:0x2b22b68>
irb(main):012:0> a.method(‘foo’).id
=> 22610640
irb(main):013:0> a.method(‘foo’).id
=> 22605804
irb(main):014:0> a.method(‘foo’).id
=> 22600968

Each time you request the Method object for a, the interpreter returns a
different wrapper object. My guess is that there is an internal map
between the method -names and the actual method objects. When you call a
method on an object, the interpreter looks at the map and send the
message to the right actual method. An alias is just another entry
pointing to the same actual method object.

The parser part of part of the interpreter will know to transform “2 +
5” to “2.+(5)” because it has rules to deal with operators. It doesn’t
have a rule to deal with a call like “2 plus 5” that doesn’t seem to
have an explicit receiver.

Disclaimer: The above may be total rubbish. If someone knows better,
please! Correct me! :slight_smile:

HTH,
Assaph

···

-----Original Message-----
From: Robert Peirce [mailto:bob@peirce-family.com]
Sent: Friday, 5 March 2004 12:40 PM
To: ruby-talk ML
Subject: Re: More alias problems

On Mar 4, 2004, at 8:03 PM, Mehr, Assaph (Assaph) wrote:

Ruby aliases are just making an object respond to a different name
with an existing method; not expanded like shell aliases.

If I understand correctly when the parser sees: 2 + 5
It actually performs: 2.+ 5
i.e. send the ‘+’ method to the FixNum object for 2, with a parameter
of 5.

Okay, but why doesn’t it do the same when it sees 2 plus 5? 2.plus(5)
and 2.plus 5 both work but 2 plus 5 does not.
I’m not saying it should; I’m just wondering why it doesn’t. I don’t
really understand what is going on when you use
aliases.