【发布时间】:2014-08-25 22:16:52
【问题描述】:
在 Ruby 中,如何从父类访问调用类的常量?
Module Foo
class Base
def test()
#how do I access calling class's constants here?
#ex: CallingClass.SOME_CONST
end
end
end
class Bar < Foo::Base
SOME_CONST = 'test'
end
【问题讨论】:
-
您是否有很多其他类使用自己的不同常量子类化
Foo::Base?test除了常量之外的所有子类都一样吗? -
@jstim 两者都是。
test()是所有子类都应继承的方法,但依赖于每个子类之间不同的常量。