Here is what I tried:
module A
def self.method1; "method1"; end
def method2; "method2"; end
end
module B; include A; end
B.method1 # => error
B.method2 # => error
B::method1 # => error
B::method2 # => error
I want to avoid copying and pasting equivalent code between two modules. The reason I’m using modules instead of classes here is because I don’t need more than one instance of each module, as they simply hold constants (other modules, at this point).
What is the best way to solve this problem?
Plain
includeonly gives you instance methods (method2in your particular piece of code). If you want to share module-level methods – extract them to separate module andextendother modules with it:It is also possible get module-level methods by
include, but with a little twist, using hook method: