0
考虑以下几点:得到扩展类的名字
class Animal
def self.info
"This is the class '#{self.class.to_s}', and the available breeds are #{BREEDS.to_s}"
end
end
class Dog < Animal
BREEDS = %w(x y z)
end
当我打电话:
Dog.info
=> This is the class 'Class'
我期待Dog
代替Class
,如何从动物获得当前的类名没有把info
在Dog
类中。
而且,我得到undefined constant Animal::BREEDS
我失去了什么?
嘛,'hello'是一个类的方法。您还没有创建“Dog”实例,因此没有任何所有者。 – 2013-02-22 22:15:47
杰克我修改了示例代码,使其更有意义,我没有在这个实例中使用 – Ryan 2013-02-22 22:18:30