r/learnruby • u/codetastik • Mar 11 '15
Inheriting parent module methods
Hi, Im trying to create a few modules, and I'd like to define some methods on a parent module that the submodules will inherit. I am currently doing this
module Parent
def self.some_method
CONSTANT.keys
end
module ChildA
CONSTANT = { word_a: "definition }
end
module ChildB
CONSTANT = { word_b: "definition }
end
endI want to be able to say
Parent::ChildA.some_method
Parent::ChildB.some_methodwhere some_method will call the submodules constant. I am currently getting an undefined method "some_method" error. I guess I assumed it would work this way because this would work with classes. I'd rather not have to include the parent module in each of the submodules because a) I would like to call the submodule off of the parent and b) it doesn't seem dry.
Any thoughts? Thanks for the help.
View all comments
Show parent comment
1
u/codetastik Mar 13 '15
You're right, what I was trying to do was not inheritance.
I'm going to go with your first suggestion. It lets me still have the same method interface(?) that I want. I was hoping to avoid having to extend the module into each Child module individually, but since that isnt possible, your first suggestion works best. Thanks.