From: Brian Candler Date: 2009-05-27T06:18:25+09:00 Subject: Re: saving values in block problem Peng Zuo wrote: > Hi,I'm using ruby 1.87. this problem seems to be the block context bug http://www.catb.org/~esr/faqs/smart-questions.html#id306810 Does this explain what you are seeing? irb(main):001:0> a = [] => [] irb(main):002:0> str = "xxx" => "xxx" irb(main):003:0> a << str => ["xxx"] irb(main):004:0> str << "yyy" => "xxxyyy" irb(main):005:0> a << str => ["xxxyyy", "xxxyyy"] irb(main):006:0> str << "zzz" => "xxxyyyzzz" irb(main):007:0> a << str => ["xxxyyyzzz", "xxxyyyzzz", "xxxyyyzzz"] irb(main):008:0> a => ["xxxyyyzzz", "xxxyyyzzz", "xxxyyyzzz"] irb(main):009:0> An array only holds references to objects. If you insert a reference to the same object multiple times into the array, then you will simply see the same object multiple times. As for why 'dup' doesn't work: you'll need to show your code. If you are maintaining state in an instance variable, dup should work. If you are maintaining state indirectly - the instance variable points to some other object which in turn holds state - then the dup'd object will still point to this same object. -- Posted via http://www.ruby-forum.com/.