Jano Svitok
10/18/2006 1:19:00 PM
On 10/18/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> Is it possible to mix in a module method to become a class method? I'd
> like to define the method_added method in a module and then mix it
> into a class like so
> module Foo
> def self.method_added(method)
> puts "Added #{method}"
> end
> end
>
> class Bar
> include Foo
> def bar
> end
> def baz
> end
> end
> So that I get
> Added bar
> Added baz
> printed out. Is there any way to do this besides making Foo a class
> and subclassing Foo from it?
>
> Farrel
See previous thread: 'Ruby for Rails p.462-464 - why include vs.
extend?' or api.rubyon rails and there *::*::ClassMethods
basicly something like this:
module A
module M
module ClassMethods
def some_method
#...
end
def included(c)
c.extend(ClassMethods)
end
end
end
and then a class later that does this:
class B
include A::M
end