# Class Method & Singletons - newbie question

**URL:** <https://rubytalk.org/t/class-method-singletons-newbie-question/53714>\
**Category:** ruby-talk\
**Created:** [3 June 2009 15:05 UTC](https://rubytalk.org/t/class-method-singletons-newbie-question/53714 "2009-06-03T15:05:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salai\_Khine](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Salai\_Khine](https://rubytalk.org/u/Salai_Khine)\
**Post date:** [3 June 2009 15:05 UTC](https://rubytalk.org/t/class-method-singletons-newbie-question/53714/1 "2009-06-03T15:05:50Z")

</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:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [3 June 2009 17:55 UTC](https://rubytalk.org/t/class-method-singletons-newbie-question/53714/2 "2009-06-03T17:55:09Z")

</div>

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

> **···**
>
> salai \<sayakyi@gmail.com\> wrote:
> 
> --  
> matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

---

<div class="post-metadata">

**Author:** ![Salai\_Khine](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Salai\_Khine](https://rubytalk.org/u/Salai_Khine)\
**Post date:** [4 June 2009 23:02 UTC](https://rubytalk.org/t/class-method-singletons-newbie-question/53714/3 "2009-06-04T23:02: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

> **···**
>
> On Wed, Jun 3, 2009 at 7:55 PM, matt neuburg\<matt@tidbits.com\> wrote:
> 
> > salai \<sayakyi@gmail.com\> 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:
> > 
> > [Just Enough Ruby](http://www.apeth.com/ruby/02justenoughruby.html)
> > 
> > Hope this helps.
> > 
> > --  
> > matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> > Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> > AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> > Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)
