# Singleton aliases

**URL:** <https://rubytalk.org/t/singleton-aliases/40468>\
**Category:** ruby-talk\
**Created:** [7 September 2007 18:06 UTC](https://rubytalk.org/t/singleton-aliases/40468 "2007-09-07T18:06:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolay\_Pavlov](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@Nikolay\_Pavlov](https://rubytalk.org/u/Nikolay_Pavlov)\
**Post date:** [7 September 2007 18:06 UTC](https://rubytalk.org/t/singleton-aliases/40468/1 "2007-09-07T18:06:01Z")

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

> **···**
>
> # --
> 
> # - Best regards, Nikolay Pavlov. \<\<\<-----------------------------------

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [7 September 2007 18:15 UTC](https://rubytalk.org/t/singleton-aliases/40468/2 "2007-09-07T18:15:09Z")

</div>

class Foo  
&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;p "each called for #{self}"  
&nbsp;&nbsp;&nbsp;&nbsp;yield  
&nbsp;&nbsp;end  
end

f1 = Foo.new

def f1.some(&block)  
&nbsp;&nbsp;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  
&nbsp;&nbsp;alias\_method :yow, :each  
end

f2.yow{ p 'simpler' }  
#=\> "each called for #\<Foo:0x7ffa6e30\>"  
#=\> "simpler"

> **···**
>
> On Sep 7, 12:06 pm, Nikolay Pavlov \<qpa...@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [7 September 2007 18:19 UTC](https://rubytalk.org/t/singleton-aliases/40468/3 "2007-09-07T18:19:04Z")

</div>

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

> **···**
>
> On 9/7/07, Nikolay Pavlov \<qpadla@gmail.com\> 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?

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [7 September 2007 18:30 UTC](https://rubytalk.org/t/singleton-aliases/40468/4 "2007-09-07T18:30:04Z")

</div>

Adding one more option to my last post:

class Foo  
&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;p "each called for #{self}"  
&nbsp;&nbsp;&nbsp;&nbsp;yield  
&nbsp;&nbsp;end  
end

f1 = Foo.new

def f1.some(&block)  
&nbsp;&nbsp;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  
&nbsp;&nbsp;alias\_method :yow, :each  
end

f2.yow{ p 'simpler' }  
#=\> "each called for #\<Foo:0x7ffa6e30\>"  
#=\> "simpler"

module BonkerTime  
&nbsp;&nbsp;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"

> **···**
>
> On Sep 7, 12:06 pm, Nikolay Pavlov \<qpa...@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![Nikolay\_Pavlov](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@Nikolay\_Pavlov](https://rubytalk.org/u/Nikolay_Pavlov)\
**Post date:** [7 September 2007 19:03 UTC](https://rubytalk.org/t/singleton-aliases/40468/5 "2007-09-07T19:03:13Z")

</div>

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)  
&nbsp;&nbsp;klass = class \<\< self; self; end  
&nbsp;&nbsp;klass.send :alias\_method, alias\_name.to\_sym, name.to\_sym  
end

> **···**
>
> On Friday 07 September 2007 21:19:04 Wilson Bilkovich wrote:
> 
> > On 9/7/07, Nikolay Pavlov \<qpadla@gmail.com\> 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. 🙂
> 
> # --
> 
> # - Best regards, Nikolay Pavlov. \<\<\<-----------------------------------
