Ruby: Extending class definitions with modules
David Carney, 31 March 2009This took me a little while to track down, but (thanks to Nathan Weizenbaum at Unspace), I’ve got the canonical solution for extending a class’ methods (not its instance methods) with a module:
module Foo
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def foo
puts "Foo!"
end
end
end
module Bar
include Foo
end
After that, one can safely call Bar.foo