More alias problems

R

Robert Peirce

--Apple-Mail-9--791126556
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=ISO-8859-1;
format=flowed

I am missing something important about aliases. I mentioned the "alias=20=

: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=20
same as :`. Is it that ruby aliases are not the same as shell aliases?


Bob Peirce Venetia, PA =20=

724-941-6883
(e-mail address removed) [HOME (Mac)]
(e-mail address removed) [OFFICE]

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

--Apple-Mail-9--791126556--
 
P

Peter

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?

You're mixing up syntax and semantics. Syntactically, + is treated as an
operator which you can use in infix notation. plus however is not. But
both are methods, and they can be aliased as methods, but plus can never
become an operator. The use of operators is just syntactic sugar, a nice,
friendly way to write things so you don't have to write 1.+(2) all the
time but instead you can write the more readable 1+2 . Aliasing has no
effect on the syntax, 'alias plus +' just creates a new method called
plus that gets the same implementation as +. So it is more or less
equivalent to this:

class Fixnum
def plus(a)
self + a
end
end

That's it. And you can't call this method using infix notation, only using
dot notation. I think you expect plus to simply be replaced with + in '1
plus 2', but it doesn't work like that. It basically just copies a given
method and gives it a new name. The same is true for the backtick, you
call it using a special syntax, but it is only a method call and aliasing
it doesn't get the alias the special syntax.

Just food for thought... How would you expect this to work out:

class Fixnum
alias plus +
end

def test(a, b)
a plus b
end

test(1, 2)
test("a", "b")

Given that plus is an alias for + for Fixnums,
 
P

Peter

class Fixnum
alias plus +
end

def test(a, b)
a plus b
end

test(1, 2)
test("a", "b")

Given that plus is an alias for + for Fixnums,

Wanted to cancel after Hal's reply, but sent it by accident (x and c are
so close together...) Anyway, just wanted to say that test would need to
be acceptable if called with Fixnums and not when called with strings.
It's because of this inconsistency that Ruby can't allow "1 plus 2".

However look at Prolog which does allow definition of any operators, with
precedence, associativity and all. But you define something to be an
operator for ALL code, not just for Fixnums.

Peter
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top