【发布时间】:2011-04-16 18:34:53
【问题描述】:
我知道有人说在 Ruby 中应该避免使用类变量(例如 @@class_var),而应该在类范围内使用实例变量(例如 @instance_var):
def MyClass
@@foo = 'bar' # Should not do this.
@foo = 'bar' # Should do this.
end
为什么在 Ruby 中不赞成使用类变量?
【问题讨论】:
-
我最近读了两本关于 Ruby 的书,但他们都没有提到这一点,看看你会得到什么答案很有趣:)
标签: ruby class-variables class-instance-variables