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

def method; end
=> nil

alias :method_alias :method
=> nil

defined? method
=> “method”

defined? method_alias
=> “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

On 10/14/10 4:48 PM, Ammar A. wrote:

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

On Fri, Oct 15, 2010 at 2:17 AM, Daniel B. [email protected]
wrote:

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

Thanks Daniel. That’s helpful.

Regards,
Ammar

On Fri, Oct 15, 2010 at 3:31 PM, Robert K.
[email protected] wrote:

Thanks Daniel. That’s helpful.

But which of the two is the alias?

Remains unknown, but I’m a little closer than I was.

Thanks,
Ammar

On Fri, Oct 15, 2010 at 1:03 PM, Ammar A. [email protected]
wrote:

Thanks Daniel. That’s helpful.
But which of the two is the alias?

robert

On 10/15/10 6:31 AM, Robert K. wrote:

Thanks Daniel. That’s helpful.

But which of the two is the alias?

I don’t know of a way to tell. I asked about this a while back:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196974

Regards,

Dan

Hi

On Sat, Oct 16, 2010 at 4:19 PM, David A. Black [email protected]
wrote:

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:

alias_methods.rb · GitHub

Thanks,
Ammar

On Sat, Oct 16, 2010 at 3:19 PM, David A. Black [email protected]
wrote:
In 1.9.2, anyway, #instance_methods seems to list methods in the
order

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

On Mon, Oct 18, 2010 at 5:56 PM, Robert D. [email protected]
wrote:

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

On Mon, Oct 18, 2010 at 5:20 PM, Ammar A. [email protected]
wrote:

That is my understanding too.
Cheers
Robert