# Getting a list of aliases

**URL:** <https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072>\
**Category:** Ruby\
**Created:** [June 11, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072 "2006-06-11T16:19:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 11, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/1 "2006-06-11T16:19:52Z")

</div>

Hi all,

Is there a way to get a list of instance method aliases somehow?

# Hypothetical syntax

Array.instance\_method\_aliases # [‘size’, ‘map’, …]

If not, I think it would be a nice little bit of extra inspection I  
could use in certain situations, like when I’m in irb and feeling too  
lazy to look at the documentation. 😉

Actually, this idea struck me when I tried to figure out just the “core”  
methods for a given class and wanted to exclude the aliases. At the  
time, I had to view the source code directly and count them. This  
proved more difficult than I thought, because sometimes methods would  
redefine functions with different names rather than use  
rb\_define\_alias() in the source code.

To give you an example of what I mean, in array.c the Array#map function  
is not declared using rb\_define\_alias(), but is simply mapped (no pun  
intended) to a different function:

rb\_define\_method(rb\_cArray, “collect”, rb\_ary\_collect, 0);  
rb\_define\_method(rb\_cArray, “map”, rb\_ary\_collect, 0);

Thoughts?

- Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 13, 2006, 9:55am UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/2 "2006-06-13T09:55:40Z")

</div>

On Jun 11, 2006, at 7:17 AM, Daniel B. wrote:

> “core” methods for a given class and wanted to exclude the  
> rb\_define\_method(rb\_cArray, “map”, rb\_ary\_collect, 0);  
> WARNING: this gets gross:

It is a little less relevant with the core classes (seeing how they  
don’t change very much), but easier to figure out what is aliased.  
You can use ParseTree to get the sexps for the class Array, and then  
it becomes pretty “obvious”:

ruby -I…/RubyInline/dev:lib -rparse\_tree -e ‘class Array;  
alias :blah :map; end; p ParseTree.new.parse\_tree(Array)’

[[:class, :Array, :Object, …  
[:defn, :blah, [:fbody, [:cfunc, 504080, 0]]], …  
[:defn, :collect, [:cfunc, 504080, 0]],  
[:defn, :collect!, [:cfunc, 504206, 0]], …  
[:defn, :map, [:cfunc, 504080, 0]],  
[:defn, :map!, [:cfunc, 504206, 0]], …  
]]

So, with c-implemented functions, your :defn is pretty much just a  
pointer to the C-function. You can compare the addresses and see  
what’s been aliased. Likewise, with a real alias to a :cfunc you can  
dig a bit deeper and see that it points to it properly.

In the case of aliases on ruby methods, it gets harder:

% ruby -I…/RubyInline/dev:lib -rparse\_tree -e ‘class X; def y; end;  
alias :x :y; end; p ParseTree.new.parse\_tree(X)’

[[:class, :X, :Object,  
[:defn, :x, [:fbody, [:scope, [:block, [:args], [:nil]]]]],  
[:defn, :y, [:scope, [:block, [:args], [:nil]]]]]]

So, while you can conclude that :fbody is a marker for an alias to  
SOMETHING, you can’t tell what because it is already expanded. You  
should be able to either compare sexps or use some C to dig in and  
see that the implementations (nd\_body pointer) have the same address.  
The code in question is:

> ```
> st_insert(RCLASS(klass)->m_tbl, name,
> (st_data_t)NEW_METHOD(NEW_FBODY(body, def, origin), orig- 
> 
> ```
> 
> > nd\_noex));

so the addresses should be the same. (yes, either option is rather  
icky).

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 13, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/3 "2006-06-13T16:43:20Z")

</div>

I am not sure if I get your question right… maybe this is what you  
need:

require ‘set’

def acts\_as\_test  
TestClasses.get \<\< self  
end

class TestClasses  
@@test\_classes = Set.new  
def self.get  
@@test\_classes  
end  
end

class TestMe  
acts\_as\_test  
end

class TestMeToo  
acts\_as\_test  
end

> TestClasses.get.inspect  
> #\<Set: {TestMeToo, TestMe}\>

-------- Original-Nachricht --------  
Datum: Tue, 13 Jun 2006 23:19:51 +0900  
Von: Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
An: removed\_email\_address@domain.invalid  
Betreff: Re: Getting a list of aliases

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 13, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/4 "2006-06-13T16:47:04Z")

</div>

sorry, wrong post ☹

-------- Original-Nachricht --------  
Datum: Tue, 13 Jun 2006 23:41:41 +0900  
Von: Peter E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
An: removed\_email\_address@domain.invalid  
Betreff: Re: Getting a list of aliases

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 13, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/5 "2006-06-13T16:20:37Z")

</div>

Ryan D. wrote:

> > lazy to look at the documentation. 😉  
> > (no pun intended) to a different function:
> 
> [[:class, :X, :Object,
> 
> > ```
> > (st_data_t)NEW_METHOD(NEW_FBODY(body, def, origin), 
> > 
> > ```
> > 
> > orig-\>nd\_noex));
> 
> so the addresses should be the same. (yes, either option is rather icky).

Very interesting Ryan, thank you.

What do you think about adding an Object.aliases method, though? Or is  
it just not worth the memory/speed hit?

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [June 13, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/getting-a-list-of-aliases/63072/6 "2006-06-13T18:54:55Z")

</div>

On Jun 13, 2006, at 7:19 AM, Daniel B. wrote:

> Very interesting Ryan, thank you.
> 
> What do you think about adding an Object.aliases method, though?  
> Or is it just not worth the memory/speed hit?

I think it is certainly doable and compact/fast enough if it is done  
in C as a standard method, but not really worth it given my ParseTree  
solution. I vote for aliased\_methods tho. 🙂
