Forum: Ruby class << self

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sijo k. (Guest)
on 2009-01-20 11:02
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
David A. Black (Guest)
on 2009-01-20 14:51
(Received via mailing list)
Hi --

On Tue, 20 Jan 2009, Sijo Kg wrote:

>         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.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
This topic is locked and can not be replied to.