------ art_40138_30533705.1178288202398 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/4/07, Brad Phelan <phelan / tttech.ttt> wrote: > > Why does this not work! > > ######################################## > module Foo > def self.foo a > puts a > end > end > > class Bar > include Foo > foo "hello" > end > > foo.rb:20: undefined method `foo' for Bar:Class (NoMethodError) > #################################### > > but this does > > class Bar > def self.foo a > puts a > end > foo "hello" > end > > > > What am I misunderstanding about modules???? > > -- > Brad > http://xtargets.com > > Module inclusion does not include singleton methods. There are a few alternatives, the clearest one IMO being: module Foo def foo a # note no self. puts a end end class Bar extend Foo # note extend, _not_include foo "hello" end The other option is to augment the include procedure. Ruby provides a hook Module#included. module Foo def self.included(including_class_or_module) class << including_class_or_module def foo a puts a end end end end class Bar include Foo foo "hello" end Usually when people do this and a module contains both singleton methods and instance methods it will look something like: module Foo def inst_meth1 end def inst_meth1 end module ClassMethods def foo a puts a end end def self.included(other) other.extend ClassMethods end end class Bar include Foo foo "hello" end Finally, you can of course use classes and plain old inheritance: class Foo def self.foo a puts a end end class Bar < Foo foo "hello" end ------ art_40138_30533705.1178288202398--