I have a Ruby class into which I want to include both class and instance methods. Following the pattern described in "Ruby Pattern: Extend through Include", I'm currently using the following:
class SomeObject
include SomeObject::Ability
def self.some_builder_method(params)
# use some_class_method ...
end
end
module SomeObject::Ability
module ClassMethods
def some_class_method(param)
# ...
end
end
def self.included(klass)
klass.extend(ClassMethods)
end
def some_instance_method
# ...
end
end
I'd rather not make two separate modules, one being included and the other being extended, because all the methods in my module logically fit together. On the other hand, this pattern requires me to:
- Define an additional
ClassMethods
module. - Write a boilerplate
self.included
method for every module.
Is this the most idiomatic way to approach this?
self.included
+.extend(ClassMethods)
is pretty much the way to do it. If you're using Rails, you can use ActiveSupport::Concern which handles this kind of thing. – Flambino Mar 9 '13 at 15:29