More alias problems

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.

HTH

···

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

I am missing something important about aliases. I mentioned the “alias
:cmd :`” problem before. I also have a problem with this:

$ cat tst2
#!/usr/bin/env ruby

class Fixnum
alias plus +
end

puts “#{2 + 5}” --> 7

puts “#{2.plus(5)}” --> 7

puts “#{2.plus 5}” --> 7

puts “#{2 plus 5}” --> ERROR

Clearly, plus is not an exact alias for ‘+’ , just as :cmd is not the
same as :`. Is it that ruby aliases are not the same as shell aliases?

Bob Peirce Venetia, PA
724-941-6883
bob@peirce-family.com [HOME (Mac)]
rbp@cooksonpeirce.com [OFFICE]

There is only one basic human right, the right to do as you damn well
please. And with it comes the only basic human duty, the duty to take
the consequences. – P.J. O’Rourke

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.

···

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.

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

Aliases aren’t macros, and the parser doesn’t have the artificial
intelligence to rewrite itself every time you use one.

An ordinary method name can only be called with the .meth notation.
That’s a syntax issue.

As a bonus, Ruby allows intuitive shorthand making certain methods
look like the operators they represent/are. So that instead of
saying 3.+(5), we’re allowed to say 3 + 5. That’s also a syntax
issue.

The mere fact that an alias refers to a method that happens to have
an infix shorthand doesn’t mean that the Ruby parser is now magically
able to recognize infix method names.

Does this help any?

Hal

···

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

Thank you. That explains the situation exactly. If I understand you
correctly, 2 + 5 is a hard-wired construct that really represents 2.+ 5
or 2.+(5). Consequently, when I alias plus to ‘+’, I am limited to
what the ruby parser can normally do, and cannot use what it may be
hard-wired to do.

···

On Mar 4, 2004, at 8:49 PM, Hal Fulton wrote:

Robert Peirce wrote:

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.

Aliases aren’t macros, and the parser doesn’t have the artificial
intelligence to rewrite itself every time you use one.

An ordinary method name can only be called with the .meth notation.
That’s a syntax issue.

As a bonus, Ruby allows intuitive shorthand making certain methods
look like the operators they represent/are. So that instead of
saying 3.+(5), we’re allowed to say 3 + 5. That’s also a syntax
issue.

The mere fact that an alias refers to a method that happens to have
an infix shorthand doesn’t mean that the Ruby parser is now magically
able to recognize infix method names.

Does this help any?