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

**URL:** <https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458>\
**Category:** ruby-talk\
**Created:** [14 October 2010 22:48 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458 "2010-10-14T22:48:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [14 October 2010 22:48 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/1 "2010-10-14T22:48:34Z")

</div>

> > 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

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [14 October 2010 23:17 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/2 "2010-10-14T23:17:57Z")

</div>

class Foo  
&nbsp;&nbsp;&nbsp;def alpha; end  
&nbsp;&nbsp;&nbsp;def beta; end  
&nbsp;&nbsp;&nbsp;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 10/14/10 4:48 PM, Ammar Ali wrote:
> 
> > > > 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.

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [15 October 2010 11:03 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/3 "2010-10-15T11:03:04Z")

</div>

Thanks Daniel. That's helpful.

Regards,  
Ammar

> **···**
>
> On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger \<djberg96@gmail.com\> 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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [15 October 2010 12:31 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/4 "2010-10-15T12:31:50Z")

</div>

But which of the two is the alias?

robert

> **···**
>
> On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali \<ammarabuali@gmail.com\> wrote:
> 
> > On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger \<djberg96@gmail.com\> 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.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [15 October 2010 12:48 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/5 "2010-10-15T12:48:21Z")

</div>

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

Thanks,  
Ammar

> **···**
>
> On Fri, Oct 15, 2010 at 3:31 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali \<ammarabuali@gmail.com\> wrote:
> > 
> > > On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger \<djberg96@gmail.com\> 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.
> > 
> > But which of the two is the alias?

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [16 October 2010 12:47 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/6 "2010-10-16T12:47:51Z")

</div>

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](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196974)

Regards,

Dan

> **···**
>
> On 10/15/10 6:31 AM, Robert Klemme wrote:
> 
> > On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali\<ammarabuali@gmail.com\> wrote:
> > 
> > > On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger\<djberg96@gmail.com\> wrote:
> > > 
> > > > class Foo  
> > > > &nbsp;&nbsp;def alpha; end  
> > > > &nbsp;&nbsp;def beta; end  
> > > > &nbsp;&nbsp;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.
> > 
> > But which of the two is the alias?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [16 October 2010 13:19 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/7 "2010-10-16T13:19:18Z")

</div>

Hi --

> > > 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.
> 
> But which of the two is the alias?

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).

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:

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  
=\> nil ruby-1.9.2-p0 \> C.instance\_methods  
=\> [:x, :a, :b, :d, :c, :f, :e, ...]

David

> **···**
>
> On Fri, 15 Oct 2010, Robert Klemme wrote:
> 
> > On Fri, Oct 15, 2010 at 1:03 PM, Ammar Ali \<ammarabuali@gmail.com\> wrote:
> > 
> > > On Fri, Oct 15, 2010 at 2:17 AM, Daniel Berger \<djberg96@gmail.com\> wrote:
> 
> --  
> David A. Black, Senior Developer, Cyrus Innovation Inc.
> 
> &nbsp;&nbsp;&nbsp;The Ruby training with Black/Brown/McAnally  
> &nbsp;&nbsp;&nbsp;Compleat Stay tuned for next event!  
> &nbsp;&nbsp;&nbsp;Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [17 October 2010 18:42 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/8 "2010-10-17T18:42:07Z")

</div>

> 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:

&nbsp;&nbsp;[631102’s gists · GitHub](http://gist.github.com/631102)

Thanks,  
Ammar

> **···**
>
> On Sat, Oct 16, 2010 at 4:19 PM, David A. Black \<dblack@rubypal.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [18 October 2010 14:56 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/9 "2010-10-18T14:56:53Z")

</div>

\<snip\> 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:
> 
> 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  
> =\> nil ruby-1.9.2-p0 \> C.instance\_methods  
> =\> [: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  
&nbsp;&nbsp;def a; end  
&nbsp;&nbsp;alias\_method :x, :a  
&nbsp;&nbsp;remove\_method :a  
&nbsp;&nbsp;alias\_method :a, :x  
&nbsp;&nbsp;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 Sat, Oct 16, 2010 at 3:19 PM, David A. Black \<dblack@rubypal.com\> wrote:
> 
> > --  
> > David A. Black, Senior Developer, Cyrus Innovation Inc.
> > 
> > The Ruby training with Black/Brown/McAnally  
> > Compleat Stay tuned for next event!  
> > Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)
> 
> --  
> There are worse things than having people misunderstand your work. A  
> worse danger is that you will yourself misunderstand your work.  
> -- Paul Graham

---

<div class="post-metadata">

**Author:** ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)\
**Post date:** [18 October 2010 15:20 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/10 "2010-10-18T15:20:45Z")

</div>

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:56 PM, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > 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 ;).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [19 October 2010 06:16 UTC](https://rubytalk.org/t/is-it-possible-to-find-out-if-an-identifier-is-a-method-alias/60458/11 "2010-10-19T06:16:22Z")

</div>

That is my understanding too.  
Cheers  
Robert

> **···**
>
> On Mon, Oct 18, 2010 at 5:20 PM, Ammar Ali \<[ammarabuali@gmail.com](mailto:ammarabuali@gmail.com)\> wrote:
> 
> --  
> There are worse things than having people misunderstand your work. A  
> worse danger is that you will yourself misunderstand your work.  
> -- Paul Graham
