From: Phrogz Date: 2007-11-23T12:20:00+09:00 Subject: Re: Class Methods from Modules 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: module Color module ClassMethods def initialize_colors p "woot!" end end def giggle puts "tee hee!" end def self.included(receiver) receiver.extend ClassMethods end end class Shape include Color initialize_colors #=> "woot!" end Shape.new.giggle #=> "tee hee!"