无法从ruby中的父类访问常量

时间:2013-01-02 09:13:08

标签: ruby

我试图从父类访问常量但是得到错误

NameError:未初始化的常量CONSTS

这是我的代码 - 父类

module Abc
  class Xyz
    class A
      class CONSTS
        ONE = "1"
        TWO = "2"
        Three = "3"
      end
    end
  end
end

这是儿童班

module Abc
  class Xyz
    class B < A
      class << self
        def print_const
          get_const
        end

        private

        def get_const
          puts CONSTS::ONE
        end
      end
    end
  end
end

现在,当我尝试Abc:Xyz :: B.print_const时,我收到了上述错误。

谁能告诉我我哪里做错了?

2 个答案:

答案 0 :(得分:0)

尝试使用a::CONSTS:ONE或完整标识符::abc::xyz::a::CONSTS:ONE

答案 1 :(得分:0)

你可以这样使用

...
def get_const
  puts self.class.superclass::CONSTS::ONE
end
...