Hello I wan't to design a Module that defines some methods that I wan't as class methods in the classes that includes them. Is it possible? Thanks
on 2007-07-13 16:19
on 2007-07-13 16:43
On 7/13/07, Pedro Cardoso <firstname.lastname@example.org> wrote: > Hello > > I wan't to design a Module that defines some methods that I wan't as > class methods in the classes that includes them. Is it possible? > > Thanks > module MyModule module ClassMethods def class_method_one( ) puts "you're in class method one" end def class_method_two( ) puts "you're in class method two" end end # module ClassMethods def self.included( other ) other.extend ClassMethods if Class === other end def method_one( ) puts "you're in method one" end end class C include MyModule end C.new.method_one #=> you're in method one C.class_method_one #=> you're in class method one C.class_method_two #=> you're in class method two Blessings, TwP
on 2007-07-13 17:04
Hi. Thanks! I found another way. Module MyModule def m end end class MyClass extend MyDodule end MyClass.m Can anyone explain the "job" of extend? Thanks.
on 2007-07-13 17:17
Pedro Cardoso wrote: > Hi. Thanks! > > I found another way. > > Module MyModule > def m > end > end > > class MyClass > extend MyDodule > end > > MyClass.m > > Can anyone explain the "job" of extend? > > Thanks. Extend includes the methods in the singleton class. E.g. MyClass.extend(MyModue) is (almost) equivalent to: class <<MyClass include MyModule end Only almost because iirc a different callback is used. Regards Stefan