# Class#aliases?

**URL:** <https://rubytalk.org/t/class-aliases/41306>\
**Category:** ruby-talk\
**Created:** [11 October 2007 04:58 UTC](https://rubytalk.org/t/class-aliases/41306 "2007-10-11T04:58:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [11 October 2007 04:58 UTC](https://rubytalk.org/t/class-aliases/41306/1 "2007-10-11T04:58:41Z")

</div>

Does this method exist? I think that it doesn't, but I just thought  
I'd make sure.

> **···**
>
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [11 October 2007 05:37 UTC](https://rubytalk.org/t/class-aliases/41306/2 "2007-10-11T05:37:29Z")

</div>

Giles Bowkett wrote:

> Does this method exist? I think that it doesn't, but I just thought  
> I'd make sure.
> 
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

It is not necessary, because Classe are objects...

irb(main):001:0\> class Otto  
irb(main):002:1\> def hi  
irb(main):003:2\> puts "Hi!"  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> Hugo = Otto  
=\> Otto  
irb(main):007:0\> Otto.new.hi  
Hi!  
=\> nil  
irb(main):008:0\> Hugo.new.hi  
Hi!  
=\> nil

Wolfgang Nádasi-Donner

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Konrad\_Meyer](https://avatars.discourse-cdn.com/v4/letter/k/df705f/32.png) [@Konrad\_Meyer](https://rubytalk.org/u/Konrad_Meyer)\
**Post date:** [11 October 2007 05:39 UTC](https://rubytalk.org/t/class-aliases/41306/3 "2007-10-11T05:39:18Z")

</div>

Quoth Giles Bowkett:

> Does this method exist? I think that it doesn't, but I just thought  
> I'd make sure.
> 
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

Irb says no, but I'd guess you'd have already tried that.

> **···**
>
> --  
> Konrad Meyer \<konrad@tylerc.org\> [http://konrad.sobertillnoon.com/](http://konrad.sobertillnoon.com/)

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [11 October 2007 07:46 UTC](https://rubytalk.org/t/class-aliases/41306/4 "2007-10-11T07:46:08Z")

</div>

Giles Bowkett wrote:

> Does this method exist? I think that it doesn't, but I just thought  
> I'd make sure.
> 
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

Again - it cannot exist. The syntactic element "alias" cannot be applied  
to local variables, instance variables, class variables and constants -  
because class names are (usually) constants, they can't have aliases. If  
you use normal assignment to constants for aliasing classes a simple  
method can be defined...

class Class  
&nbsp;&nbsp;def aliases?  
&nbsp;&nbsp;&nbsp;&nbsp;Module.constants.find\_all{|c|self.equal?(const\_get(c))}  
&nbsp;&nbsp;end  
end

Otto = String  
Hugo = String  
p String.aliases? # =\> ["Hugo", "String", "Otto"]

Wolfgang Nádasi-Donner

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [11 October 2007 14:27 UTC](https://rubytalk.org/t/class-aliases/41306/5 "2007-10-11T14:27:48Z")

</div>

Do you mean that you'd like a list of all aliases defined in a class,  
similar to #instance\_methods?

T.

> **···**
>
> On Oct 10, 9:58 pm, "Giles Bowkett" \<gil...@gmail.com\> wrote:
> 
> > Does this method exist? I think that it doesn't, but I just thought  
> > I'd make sure.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [11 October 2007 10:12 UTC](https://rubytalk.org/t/class-aliases/41306/6 "2007-10-11T10:12:52Z")

</div>

Wolfgang Nádasi-Donner wrote:

> Giles Bowkett wrote:
> 
> > ...

btw - is there a difference between "alias" and "alias\_method" if  
applied to a method name (I don't mean that I must use Symbols in  
"alias\_method)?

Wolfgang Nádasi-Donner

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yossef\_Mendelssohn](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yossef\_Mendelssohn](https://rubytalk.org/u/Yossef_Mendelssohn)\
**Post date:** [11 October 2007 14:30 UTC](https://rubytalk.org/t/class-aliases/41306/7 "2007-10-11T14:30:59Z")

</div>

It seems you and I understood the question in two different ways. Only  
Giles can answer this for certain, but I don't think you're answering  
the question he asked.

I understood the point of Class#aliases to mean not "Are there any  
other constants out there that are really the same class object, and  
if so, what are they?" but "Are there any methods in this class that  
are aliases for other methods, and if so, what are the connections?"

Giles, please explain.

> **···**
>
> On Oct 11, 2:46 am, "Wolfgang Nádasi-Donner" \<ed.oda...@wonado.de\> wrote:
> 
> > Giles Bowkett wrote:  
> > \> Does this method exist? I think that it doesn't, but I just thought  
> > \> I'd make sure.
> > 
> > \> --  
> > \> Giles Bowkett
> > 
> > \> Blog:[http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> > \> Portfolio:[http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> > \> Tumblelog:[http://giles.tumblr.com/](http://giles.tumblr.com/)
> > 
> > Again - it cannot exist. The syntactic element "alias" cannot be applied  
> > to local variables, instance variables, class variables and constants -  
> > because class names are (usually) constants, they can't have aliases. If  
> > you use normal assignment to constants for aliasing classes a simple  
> > method can be defined...
> > 
> > class Class  
> > &nbsp;&nbsp;def aliases?  
> > &nbsp;&nbsp;&nbsp;&nbsp;Module.constants.find\_all{|c|self.equal?(const\_get(c))}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Otto = String  
> > Hugo = String  
> > p String.aliases? # =\> ["Hugo", "String", "Otto"]
> > 
> > Wolfgang Nádasi-Donner
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.
> 
> --  
> -yossef

---

<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:** [18 October 2007 11:21 UTC](https://rubytalk.org/t/class-aliases/41306/8 "2007-10-18T11:21:25Z")

</div>

Trans wrote:

> > Does this method exist? I think that it doesn't, but I just thought  
> > I'd make sure.
> 
> Do you mean that you'd like a list of all aliases defined in a class,  
> similar to #instance\_methods?

If so, I brought this up a while back:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196740](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196740)

Ryan Davis came up with a rather interesting solution. 🙂

One problem right off the bat, however, is that the Ruby source would need to be modified to be more stringent about using true aliases in order such a method to be accurate.

Regards,

Dan

> **···**
>
> > On Oct 10, 9:58 pm, "Giles Bowkett" \<gil...@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [11 October 2007 19:21 UTC](https://rubytalk.org/t/class-aliases/41306/9 "2007-10-11T19:21:31Z")

</div>

> Giles, please explain.
> 
> --  
> -yossef

I was skimming, and read that as "Giles, please explain yourself."

Anyway -

> Do you mean that you'd like a list of all aliases defined in a class,  
> similar to #instance\_methods?

Actually no. I was looking at an error message that expected #aliases  
to exist, and I thought it was weird.

> I understood the point of Class#aliases to mean not "Are there any  
> other constants out there that are really the same class object, and  
> if so, what are they?" but "Are there any methods in this class that  
> are aliases for other methods, and if so, what are the connections?"

Nope, it was just a weird error message referencing some internal  
code. But both these interpretations are pretty interesting.

Technically, as I learned from Ryan Davis, you \*\*can\*\* actually  
"alias" a class, even though you can't \*\*alias\*\* a class.

> > Monkey = String

=\> String

> > m = Monkey.new("asdf")

=\> "asdf"

> > m.gsub!(/asdf/, "qwerty")

=\> "qwerty"

> **···**
>
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)
