0

I have a base module that has desirable class defined on it like so:

module Base
  class Desirable
  end
end

Now inside a nested module chain (in Base::Nested1) I would like to derive from this class. I first tried to do this like so:

module Base::Nested1
  class Derived < Desirable
  end
end

this throws an error:

uninitialized constant Base::Nested1::Desirable

If I declare it like this however:

module Base
  module Nested1
    class Derived < Desirable
    end
  end
end

I get no errors.

The errors are caused by this section of the const_missing method (in rails activesupport 5.0.1):

517       elsif (parent = from_mod.parent) && parent != from_mod &&
518             ! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
519         # If our parents do not have a constant named +const_name+ then we are free
520         # to attempt to load upwards. If they do have such a constant, then this
521         # const_missing must be due to from_mod::const_name, which should not
522         # return constants from from_mod's parents.

My problem is I don't understand the comment of that code; why should we not be loading this constant from the parent module in this case?

Mike H-R
  • 7,726
  • 5
  • 43
  • 65
  • Thanks for pointing out the duplicate question, I was unable to find it. – Mike H-R Jan 27 '17 at 11:33
  • 1
    No problem :) This question (the one I marked yours as duplicate of) is among my favorite and I spent good few minutes searching for it :) I hope (sure) you have found the answer in there – Andrey Deineko Jan 27 '17 at 11:37

0 Answers0