# Need for singleton\_methods

**URL:** <https://www.ruby-forum.com/t/need-for-singleton-methods/131974>\
**Category:** Ruby\
**Created:** [March 14, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/need-for-singleton-methods/131974 "2008-03-14T20:27:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/need-for-singleton-methods/131974/1 "2008-03-14T20:27:07Z")

</div>

I have trying to learn about this class\<\<self;self;end and the  
class\<\<self followed by defining the method.I absolutely don’t see  
anything which will help this case.I can very well do it directly on the  
class …  
class Foo  
def Foo.bar  
end  
end

what is the advantage I get by using the class \<\< self notation…

class Foo  
class\<\<self  
def bar  
end  
end  
end

This is all confusing .Can any body suggest a simple example with  
tangible result to show the advantage.I am pretty sure more  
people(nubies) will have this…

I have been doing my home work for the pass 2 days. I found that it is  
helpful in inheritance as per the [whytheluckystiff.com](http://whytheluckystiff.com) guy (why’s  
poignant guide)…I don’t understand what is the need for even in that  
case.If I define the method directly like this

class Foo  
def Foo.bar  
end  
end

class Hoo \< Foo  
end

I am still OK …Hoo.bar will work…  
Any body want to pitch thier thoughts…  
THnks  
CHinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/need-for-singleton-methods/131974/2 "2008-03-14T20:50:12Z")

</div>

On Mar 14, 3:26 pm, Chinna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Foo  
> class\<\<self  
> def bar  
> end  
> end  
> end

If you want to use a helper method, like #attr\_accessor, for instance,  
this form becomes more convenient:

class Foo  
class\<\<self  
attr\_accessor :bar  
end  
end

> def Foo.bar  
> end  
> end

Better:

class Foo  
def self.bar  
end  
end

If you ever change then name of the module, you won’t have to change  
it all over the place.

T.

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/need-for-singleton-methods/131974/3 "2008-03-14T21:10:50Z")

</div>

Hi –

On Sat, 15 Mar 2008, Chinna K. wrote:

> class Hoo \< Foo  
> end
> 
> I am still OK …Hoo.bar will work…  
> Any body want to pitch thier thoughts…

The main thing that’s going on here is that objects are getting the  
ability to grow on an individual basis, and that’s a central principle  
of Ruby. The way they grow is through the acquisition of methods that  
other objects of their class don’t have.

The point of the singleton class is to provide a uniform interface to  
the various layers of behavior that make up the object. Whenever you  
want to add, override, or undefine methods, you can do it inside a  
class definition block – either class C, or class \<\< object.

Also, keep in mind that (as per that last example) it’s not just for  
class objects. It’s a general mechanism for implementing per-object  
behavior.

David

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/need-for-singleton-methods/131974/4 "2008-03-14T21:17:47Z")

</div>

This cannot be a reason since if you make change to Module then you have  
change it place where you call as well…say for eg you change Math  
Module name Maths… then where ever you are calling Math.sin would have  
to be Maths.sin

> Better:
> 
> class Foo  
> def self.bar  
> end  
> end
> 
> If you ever change then name of the module, you won’t have to change  
> it all over the place.
> 
> T.
