I do not understand why this works.
module base attr_reader: first def set @first = 1 end end module addon attr_reader: second def setup # super @second = 2 end end class test include addon def initialize (num) @num = num setup end end = a test.new (1) p one first p a.second Actually I have the "base" module, which sets up some stuff. I also have an Aden module, which sets some more things if a class wants to include it.
Now when I test it, if I do not have this super call, then zero 2 I have a super call, so i
1 2 what is really super here? It is calling the setup method from the base module, even when the base and aden are not related.
This is the first time I used to refer to the Super Module. Earlier, I have always used supercalls with classes and thought it was going to the heritage tree just to find a parent with the same method.
It contains several modules, which is also establishing any kind of heritage tree? / P>
Edit: For reference, the addon module will never be included without the base module, and the base module will always be included before any additional Addon module.
Yes, when you include the module, they are injected into a legacy chain < Pre> test.ancestors # = & gt; Existing setup to test, addon, base, object, kernel, basic object] Addon module, when included Redefines base . If you want to call the version of base , then use super .
Comments
Post a Comment