# Class Method & Singletons - newbie question

**URL:** <https://www.ruby-forum.com/t/class-method-singletons-newbie-question/169428>\
**Category:** Ruby\
**Created:** [June 3, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/class-method-singletons-newbie-question/169428 "2009-06-03T17:07:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [June 3, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/class-method-singletons-newbie-question/169428/1 "2009-06-03T17:07:06Z")

</div>

Dear Freinds,

Class Method and Singletons are the same?

Can you explain me with some simple code.

Many thanks in advance,

regards,  
salai.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [June 3, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/class-method-singletons-newbie-question/169428/2 "2009-06-03T19:56:19Z")

</div>

salai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Class Method and Singletons are the same?

No, not quite.

> Can you explain me with some simple code.

It’s very simple. There are really no class methods; there are only  
instance methods. It is convenient to talk about class methods, but in  
Ruby things are very elegant: a class is itself an instance (of the  
Class class), and so a “class method” is merely an instance method of  
that instance (i.e. an instance method of an instance of the Class  
class).

Okay, so when you define a class method by saying

class C  
end  
def C.my\_method  
end

…you are doing EXACTLY the same thing that you are doing when you  
define an instance method on any individual instance:

thing = Object.new  
def thing.my\_method  
end

Exactly WHAT same thing? You are opening the instance and defining an  
instance method applicable to that instance only. That is the  
“singleton”. So when you define a class method you are actually creating  
a singleton instance method on that Class instance.

m.

PS By the way, in a completely different context (a proposed book about  
rb-appscript) I have written an “introduction to Ruby” chapter that  
might help you with these concepts. It’s here:

[http://www.apeth.com/ruby/02justenoughruby.html](http://www.apeth.com/ruby/02justenoughruby.html)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [June 5, 2009, 1:03am UTC](https://www.ruby-forum.com/t/class-method-singletons-newbie-question/169428/3 "2009-06-05T01:03:22Z")

</div>

Hi matt,

thanks you very much for your explanation and your “Ruby introduction”  
book link.  
[http://www.apeth.com/ruby/02justenoughruby.html](http://www.apeth.com/ruby/02justenoughruby.html)

regards,  
salai
