From: Rick DeNatale Date: 2009-05-26T00:49:01+09:00 Subject: Re: Class methods in a module? On Mon, May 25, 2009 at 11:07 AM, Max Williams wrote: > At Scotland On Rails a few months ago, I was shown a nice way to put > class methods in a module, alongside instance methods, in such a way as > they will get added as class methods automatically, without any work > required on the part of the person including the module. > > Unfortunately i failed to make a note of how to do this and i've since > forgotten.  Can anyone show me? > > It was something along these lines...(the following doesn't work btw) > > module Mappable > >   #instance methods >   def foo >     "foo" >   end > >   #class methods >   class << self.class >     def bar >       "bar" >     end >   end > > end > > can anyone set me straight? > thanks > max The usual way is to use the hook method Module#included, something like: module Mappable #instance methods def foo "foo" end #class methods module ClassMethods def bar "bar" end end def self.included(other) other.extend(ClassMethods) end end By the way this nesting of a Module inside a module is another variant on something discussed in another recent ruby-talk thread. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale