我试图从父类访问常量但是得到错误
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时,我收到了上述错误。
谁能告诉我我哪里做错了?
答案 0 :(得分:0)
尝试使用a::CONSTS:ONE
或完整标识符::abc::xyz::a::CONSTS:ONE
。
答案 1 :(得分:0)
你可以这样使用
...
def get_const
puts self.class.superclass::CONSTS::ONE
end
...