[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class Method & Singletons - newbie question.

Salai Khine

6/3/2009 3:06:00 PM

Dear Freinds,

Class Method and Singletons are the same?

Can you explain me with some simple code.

Many thanks in advance,


regards,
salai.

2 Answers

matt

6/3/2009 5:54:00 PM

0

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:

http://www.apeth.com/ruby/02justenoug...

Hope this helps.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Salai Khine

6/4/2009 11:02:00 PM

0

Hi matt,

thanks you very much for your explanation and your "Ruby introduction"
book link.
http://www.apeth.com/ruby/02justenoug...


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:
>
> http://www.apeth.com/ruby/02justenoug...
>
> Hope this helps.
>
> --
> matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
> Leopard - http://www.takecontrolbooks.com/leopard-custom...
> AppleScript - http://www.amazon.com/gp/product/...
> Read TidBITS! It's free and smart. http://www.t...
>
>