Hi --

On Fri, 14 Jul 2006, bwv549 wrote:

>>    class C
>>      class << self
>>        attr_accessor 'class_constant'
>>      end
>>    end
>
> For an intermediate ruby user, could you explain what is happening in
> the line:  'class << self'

The expression:

   class << obj

puts you in a class definition block for the singleton class of obj.
The singleton class of obj is where obj's singleton methods live --
that is, the methods that only obj can call.

   obj = Object.new
   def obj.x
     "Singleton method on obj"
   end

   class << obj
     puts instance_methods(false) # methods defined in this class only
   end

Output:  x

You can also define methods inside the class definition block, of
course:

   class << obj
     def y
       "Another singleton method on obj"
     end
   end


David

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black     => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack / wobblini.net              => me