【发布时间】:2014-10-08 14:27:52
【问题描述】:
我以前在代码中看到过这一点,我刚刚在 David A. Black 的 The Well Grounded Rubyist 中读到了它,但是没有用例示例可以帮助我理解为什么有人想要在像这样的类:
class Vehicle
class << self
def all_makes
# some code here
end
end
end
类上的上述单例方法与通常的类方法有何不同,如下所示:
class Vehicle
def self.all_makes
# some code here
end
end
【问题讨论】:
-
这个问题是重复的,但是关于那个问题的答案肯定还有一些不足之处。