看到一个例子,是有这个问题还是我理解不到位呢?
class LoadPaths
# ...
def initialize
@paths = []
end
def push(*paths)
@paths.push(*paths)
end
def inspect
p @paths
end
end
a = LoadPaths.new
x = a.push(1)
x.push 2
a.inspect
迷茫2017-04-22 09:00:46
這樣完全沒有問題啊.
容易引起問題的是這種:
class A
def count
@count ||= 0
@count += 1
end
end
a = A.new
a.count # => 1
a.instance_variable_set(:@count, 10)
a.count # => 11
其中instance_variable_set
可以修改物件內的實例變數 ~ ~
當然, 也可以理解為 Ruby 的強大之處!!!