Why doesn't Ruby find classes in a higher scope when module is specified using ::?

后端 未结 2 909
感情败类
感情败类 2020-12-06 14:38

I just got stuck on this for a while. Take this base:

module Top
  class Test
  end

  module Foo
  end
end

Later, I can define classes ins

2条回答
  •  无人及你
    2020-12-06 14:49

    This is expected. Using :: changes the scope of constant lookup and expects Test to be defined under Top::Foo.

    To get the expected result, you could write:

    module Top::Foo
      class SomeTest < Top::Test
      end
    end
    

    or:

    module Top
      class Foo::SomeTest < Test
      end
    end
    

    or even:

    class Top::Foo::SomeTest < Top::Test
    end
    

提交回复
热议问题