From: Jean-Hugues ROBERT Date: 2002-05-05T00:29:20+09:00 Subject: Re: Class methods defined in a module, how-to ? At 23:31 04/05/2002 +0900, you wrote: >Jean-Hugues ROBERT wrote: > > > > At 20:31 04/05/2002 +0900, you wrote: > > > module MyModule > > > def foo() > > > print "foo" > > > end > > > end > > > class MyClass > > > extend MyModule > > > end > > > MyClass.foo() # work :-) > > > > > > > > >Guy Decoux > > > > Thanks. However I would like to *include* the module, not extending it. > >You aren't extending the Module. >You are extending MyClass with the contents of the module. True, sorry for the confusion. >Sounds like you need to split up your module in a part you want to >include (instance methods) and a part you want to extend your class with >(class methods). Yes, that would do the job. Maybe there is a way to get by with a single module. I was wondering if some "included()" hook existed (similar to inherited() for classes), and I found that "append_features()" is the hook called when a module is included. So: What I intend (to include class methods defined in a module) module MyModule def MyModule.foo() end end class MyClass include MyModule # I want to include both class & instance methods end MyClass.foo() # Calls foo() class method defined in module, broken. Could be written: module MyModule def append_features( includer ) super( includer) includer.instance_eval( " # Here comes the definitions of included class methods def foo() end ") end end class MyClass include MyModule end MyClass.foo() However this does not work either... apparently append_features() is not called. Additionnal 1) the construct is ugly, relies on eval and 2) there is no way to later on introspect MyClass and know that foo() class method was defined in MyModule. Any hints ? Jean-Hugues