# Singleton aliases

**URL:** <https://www.ruby-forum.com/t/singleton-aliases/114090>\
**Category:** Ruby\
**Created:** [September 7, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090 "2007-09-07T20:07:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [September 7, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090/1 "2007-09-07T20:07:30Z")

</div>

Hello all.  
I have a class with instance method “each” and i want to add variable  
aliases for it based on some preferences of the generated objects.  
Did some one know how i can achieve this?

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [September 7, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090/2 "2007-09-07T20:17:31Z")

</div>

On Sep 7, 12:06 pm, Nikolay P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a class with instance method “each” and i want to add variable  
> aliases for it based on some preferences of the generated objects.

class Foo  
def each  
p “each called for #{self}”  
yield  
end  
end

f1 = Foo.new

def f1.some(&block)  
each(&block)  
end

f1.each{ p ‘hi!’ }  
#=\> “each called for #Foo:0x7ffa72cc”  
#=\> “hi!”

f1.some{ p ‘aliased’ }  
#=\> “each called for #Foo:0x7ffa72cc”  
#=\> “aliased”

f2 = Foo.new  
class \<\< f2  
alias\_method :yow, :each  
end

f2.yow{ p ‘simpler’ }  
#=\> “each called for #Foo:0x7ffa6e30”  
#=\> “simpler”

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [September 7, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090/3 "2007-09-07T20:20:31Z")

</div>

On 9/7/07, Nikolay P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all.  
> I have a class with instance method “each” and i want to add variable  
> aliases for it based on some preferences of the generated objects.  
> Did some one know how i can achieve this?

This is one way. There are several.  
I am assuming you mean that only that particular instance should have  
this method, and not other instances of the same class.

metaclass = class \<\< self;self;end  
sym = :some\_name\_you\_generated  
metaclass.send :alias\_method, sym, :each

More traditionally, this is usually a good place to consider using  
inheritance. 🙂

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [September 7, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090/4 "2007-09-07T21:04:36Z")

</div>

On Friday 07 September 2007 21:19:04 Wilson B. wrote:

> sym = :some\_name\_you\_generated  
> metaclass.send :alias\_method, sym, :each
> 
> More traditionally, this is usually a good place to consider using  
> inheritance. 🙂

Wow. Thanks for all suggestions. But that one was really helpful and  
simple  
to me and i’ve come up to this implementation:

def singleton\_alias(alias\_name, name)  
klass = class \<\< self; self; end  
klass.send :alias\_method, alias\_name.to\_sym, name.to\_sym  
end

---

<div class="post-metadata">

**Author:** ![Nikolay\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nikolay\_P](https://www.ruby-forum.com/u/Nikolay_P)\
**Post date:** [September 7, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/singleton-aliases/114090/5 "2007-09-07T20:30:33Z")

</div>

On Sep 7, 12:06 pm, Nikolay P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a class with instance method “each” and i want to add variable  
> aliases for it based on some preferences of the generated objects.

Adding one more option to my last post:

class Foo  
def each  
p “each called for #{self}”  
yield  
end  
end

f1 = Foo.new

def f1.some(&block)  
each(&block)  
end

f1.each{ p ‘hi!’ }  
#=\> “each called for #Foo:0x7ffa72cc”  
#=\> “hi!”

f1.some{ p ‘aliased’ }  
#=\> “each called for #Foo:0x7ffa72cc”  
#=\> “aliased”

f2 = Foo.new  
class \<\< f2  
alias\_method :yow, :each  
end

f2.yow{ p ‘simpler’ }  
#=\> “each called for #Foo:0x7ffa6e30”  
#=\> “simpler”

module BonkerTime  
def go\_bonkers( &block ); each( &block ); end  
end

f3 = Foo.new  
f3.extend( BonkerTime )  
f3.go\_bonkers{ p “w00t” }  
#=\> “each called for #Foo:0x7ffa6908”  
#=\> “w00t”
