# Aliasing class methods

**URL:** <https://rubytalk.org/t/aliasing-class-methods/43874>\
**Category:** ruby-talk\
**Created:** [18 January 2008 11:52 UTC](https://rubytalk.org/t/aliasing-class-methods/43874 "2008-01-18T11:52:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [18 January 2008 11:52 UTC](https://rubytalk.org/t/aliasing-class-methods/43874/1 "2008-01-18T11:52:26Z")

</div>

Hello all,

I'm having trouble aliasing class methods. I've tried the following,  
but got errors saying the method being aliased doesn't exist for class  
"Class".

Class Foo  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;alias bar bla  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.bla  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello"  
&nbsp;&nbsp;end  
end

Foo.bar

Any suggestions? Thanks! -- BTR

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [18 January 2008 12:16 UTC](https://rubytalk.org/t/aliasing-class-methods/43874/2 "2008-01-18T12:16:20Z")

</div>

Alle Friday 18 January 2008, Bryan Richardson ha scritto:

> Hello all,
> 
> I'm having trouble aliasing class methods. I've tried the following,  
> but got errors saying the method being aliased doesn't exist for class  
> "Class".
> 
> Class Foo  
> &nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;alias bar bla  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def self.bla  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Hello"  
> &nbsp;&nbsp;end  
> end
> 
> Foo.bar
> 
> Any suggestions? Thanks! -- BTR

The alias statement is executed as soon as it's found, i.e before Foo.bla is  
defined. Since you can't alias something which doesn't exist, you get an  
error. Try switching the order:

Class Foo  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;alias bar bla  
&nbsp;&nbsp;end

&nbsp;&nbsp;def self.bla  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello"  
&nbsp;&nbsp;end  
end

Foo.bar

Stefano
