84669 人學習
152542 人學習
20005 人學習
5487 人學習
7821 人學習
359900 人學習
3350 人學習
180660 人學習
48569 人學習
18603 人學習
40936 人學習
1549 人學習
1183 人學習
32909 人學習
看到一个例子,是有这个问题还是我理解不到位呢?
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
认证高级PHP讲师
實例方法操作實例變數,有什麼問題?
這樣完全沒有問題啊. 容易引起問題的是這種:
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 的強大之處!!!
instance_variable_set
實例方法操作實例變數,有什麼問題?
這樣完全沒有問題啊.
容易引起問題的是這種:
其中
instance_variable_set
可以修改物件內的實例變數 ~ ~當然, 也可以理解為 Ruby 的強大之處!!!