Gavin Kistner wrote:
> Greg Willits wrote:
>> In this play code below, I want to use initialize_colors in either the
>> class Shape or Square as a class method.
> 
> Including a module in a class causes instance methods of the module to
> be in the lookup path for instances of the class. Extending a class
> using a module causes instance methods of the module to be in the
> lookup path for the class itself.
> 
> (See http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png)
> 
> A common idiom to be able to cause a class to get both class and
> instance methods when including a single module follows:

Cool, thanks. But...

In this code the fill_color is still never set to black. It seems to me 
that Module code even if added as class methods cannot access class 
variables??

-- gw

module Color
  module ClassMethods
    def initialize_colors
      @@fill_color = 'black'
      p "woot!"
    end
  end

  def self.included(receiver)
    receiver.extend ClassMethods
  end
end

class Shape
  include Color
  @@fill_color = 'white'
  initialize_colors

  def self.fill_color
    @@fill_color
  end
end

Shape.new
p Shape.fill_color
-- 
Posted via http://www.ruby-forum.com/.