[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

class << self

Sijo Kg

1/20/2009 9:01:00 AM

Hi
I have code like

class A
class <<self
def first
-------
end
def second
------
end
end
end

What I understood is now the def first and second becomes class
methods of class A..Am I right? Is that its only use?

Thanks in advance
Sijo
--
Posted via http://www.ruby-....

1 Answer

David A. Black

1/20/2009 12:49:00 PM

0

Hi --

On Tue, 20 Jan 2009, Sijo Kg wrote:

> Hi
> I have code like
>
> class A
> class <<self
> def first
> -------
> end
> def second
> ------
> end
> end
> end
>
> What I understood is now the def first and second becomes class
> methods of class A..Am I right? Is that its only use?

class << object puts you in a class definition block for the singleton
class of object. Creating class methods is the most common use case,
but it works on any object (other than a few built-ins that aren't
allowed to have singleton classes).


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!