From: Brian Candler Date: 2009-01-26T00:41:18+09:00 Subject: Re: Help me understand this technique from an open source app? Pito Salas wrote: > n the code at the bottom (from HTTParty), the module is included in > some other class ("O"), and among the effects is that (for example) > the method default_params becomes available in "O". Well, consider this first: module ClassMethods def foo puts "hello from class" end end module InstanceMethods def bar puts "hello from instance" end end class Parent include InstanceMethods extend ClassMethods end Parent.foo Parent.new.bar However this pattern is very common, so it's refactored in two ways. 1. put the module containing class methods *inside* the module containing the instance methods module MyTools ... instance methods go here module ClassMethods ... class methods go here end end 2. use the "included" hook so that when you include MyTools, it automatically does extend MyTools::ClassMethods at the same time. The example above becomes: module MyTools def self.included(base) base.extend ClassMethods end module ClassMethods def foo puts "hello from class" end end def bar puts "hello from instance" end end class Parent include MyTools end Parent.foo Parent.new.bar At least, I *think* that's what you were asking about. There's also the ModuleLevelInheritableAttributes there, but you didn't post all the code for that. But notice that the include hook is calling mattr_inheritable :default_options in the base class, and also setting a class instance variable. HTH, Brian. -- Posted via http://www.ruby-forum.com/.