Is it possible to find out if an identifier is a method alias?

A

Ammar Ali

def method; end
=> nil=> "method"

The above makes perfect sense, but is there a way to determine if an
identifier is an alias?

I don't have any practical use for such functionality. I'm purely
curious if there is a way to do it.

Thanks,
Ammar
 
D

Daniel Berger

=> "method"

The above makes perfect sense, but is there a way to determine if an
identifier is an alias?

I don't have any practical use for such functionality. I'm purely
curious if there is a way to do it.

class Foo
def alpha; end
def beta; end
alias gamma alpha
end

Foo.instance_method:)alpha) == Foo.instance_method:)beta) # => false
Foo.instance_method:)alpha) == Foo.instance_method:)gamma) # => true

Regards,

Dan
 
A

Ammar Ali

class Foo
=C2=A0def alpha; end
=C2=A0def beta; end
=C2=A0alias gamma alpha
end

Foo.instance_method:)alpha) =3D=3D Foo.instance_method:)beta) # =3D> fals= e
Foo.instance_method:)alpha) =3D=3D Foo.instance_method:)gamma) # =3D> tru= e

Thanks Daniel. That's helpful.

Regards,
Ammar
 
A

Ammar Ali

I could be wrong, and haven't dug into the code, but I've always had the
impression that (by design) there's no difference between different ways
to refer to a method. It's not that the alias is a kind of nickname for
the "real" name; it *is* (one of) the real name(s).

You inspired me to take a look and you are right on. alias just
creates a new entry for the same method into the target's method
table. There is nothing in the entry that distinguishes it as an
alias.
In 1.9.2, anyway, #instance_methods seems to list methods in the order
they were created/aliased, though I don't know whether this is
guaranteed:

The methods are always copied in the order they were defined (using
rb_ary_push). I guess that's guaranteed enough for a hack. Based on
the equality Daniel pointed out and the ordering you did, I took a jab
at it:

http://gist.github.com/631102

Thanks,
Ammar
 
R

Robert Dober

they were created/aliased, though I don't know whether this is
guaranteed:

ruby-1.9.2-p0 > class C; def x; end; alias a x; def b; end; def d; end;
alias c d; def f; end; def e; end; end
=3D> nil ruby-1.9.2-p0 > C.instance_methods
=3D> [:x, :a, :b, :d, :c, :f, :e, ... ]


David
Maybe it is best to say that somehow it really does not matter, I
would be curious to see where it matters. The following, completely
useless code, somehow demonstrates this:

Class::new do
def a; end
alias_method :x, :a
remove_method :a
alias_method :a, :x
p instance_methods( false )
end

and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;). But I prefer
David's notation above which is the perfect explanation of what is
really happening here.(1)
Hopefully that kind of manipulation can be safely ignored when talking
about "definition order".

(1) Maybe with the exception that remove_method "really" means, remove
the entry with this name pointing to the method, but by all means let
us keep the name :).

Cheers
Robert
--
David A. Black, Senior Developer, Cyrus Innovation Inc.

=A0The =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Ruby training with Black/Brown= /McAnally
=A0Compleat =A0 =A0 =A0 =A0 =A0 =A0 =A0Stay tuned for next event!
=A0Rubyist =A0 =A0 =A0 =A0 =A0 =A0 =A0 http://www.compleatrubyist.com



--=20
There are worse things than having people misunderstand your work. A
worse danger is that you will yourself misunderstand your work.
-- Paul Graham
 
A

Ammar Ali

Maybe it is best to say that somehow it really does not matter, I
would be curious to see where it matters. The following, completely
useless code, =C2=A0somehow demonstrates this:

Class::new do
=C2=A0def a; end
=C2=A0alias_method :x, :a
=C2=A0remove_method :a
=C2=A0alias_method :a, :x
=C2=A0p instance_methods( false )
end

and the question I would like to ask is, what is :a in this case? An
alias to itself after having been removed I guess ;).

If I understand how this works correctly, then remove_method is only
removing the :a entry for the method from the object's table, but
since there is another "reference" to the method definition (the b
alias, which is just another entry to same method), the body of the
method is not removed. Adding :a again is simply adding a new entry
for the same method. If both :a and :b are removed, then one tries to
add an alias a NameError would be raised.

Regards,
Ammar
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top