Here's the code:
module A
class C1
def self.grovel(x)
return A::helper(x) + 3
end
end
class C2
def self.grovel(x)
return A::helper(x) + 12
end
end
private
def helper(y)
y + 7
end
module_function :helper
end
f = A::C1.grovel(7)
puts f
puts A::C2.grovel(25)
I'm working with legacy code, trying to avoid changing too much. I'm not sure I would have made two separate classes with the same method, as each class only contains one method, with common code. I want to extract the common code into a method that only the methods in A can see, but still have to invoke it with its fully qualified name ("A::helper").
Is there a better way of doing this? Ideally, I'd like to wrap the common code in a method (let's still call it "helper") that can be invoked from within the class grovel methods without any qualification, but isn't easily available to code outside module A.
Thanks.
Minixs are useful, when the proper inheritance can't be applied, for example, then a class must be inherit properties of the two other classes, therefore you can just use the inheritance mechanics here: