# Saving values in block problem

**URL:** <https://www.ruby-forum.com/t/saving-values-in-block-problem/168852>\
**Category:** Ruby\
**Created:** [May 26, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/saving-values-in-block-problem/168852 "2009-05-26T17:45:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peng\_Zuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Peng\_Zuo](https://www.ruby-forum.com/u/Peng_Zuo)\
**Post date:** [May 26, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/saving-values-in-block-problem/168852/1 "2009-05-26T17:45:31Z")

</div>

Hi,I’m using ruby 1.87. this problem seems to be the block context bug  
for  
1.8x, in the results array, I always got the same value, which is the  
last  
return value of the iteration .  
I’ve got an object, which has multiple states. when I call each\_state,  
I’m  
actually changing the state.

results = []  
an\_object.each\_state do |the\_object|  
#some manipulations  
results \<\< the\_object  
end

the each\_state method looks like:

def each\_state  
#blah blah, change the current state  
yield(self) # I also tried self.dup, but it didnt work either.  
end

anyone knows how to solve this?  
TIA

Regards,

Peng Zuo

---

<div class="post-metadata">

**Author:** ![Peng\_Zuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Peng\_Zuo](https://www.ruby-forum.com/u/Peng_Zuo)\
**Post date:** [May 26, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/saving-values-in-block-problem/168852/2 "2009-05-26T18:58:14Z")

</div>

Peng Zuo wrote:

> end
> 
> the each\_state method looks like:
> 
> def each\_state  
> #blah blah, change the current state  
> yield(self) # I also tried self.dup, but it didnt work either.  
> end

I’m guessing what you want is for results[i] to be a snapshot of the  
object as it is in the i-th state?

If dup doesn’t work, maybe your object has some other objects attached  
to it in which you are storing something? In that case, try a deep copy:

Marshal.load(Marshal.dump(self))

---

<div class="post-metadata">

**Author:** ![Peng\_Zuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Peng\_Zuo](https://www.ruby-forum.com/u/Peng_Zuo)\
**Post date:** [May 26, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/saving-values-in-block-problem/168852/3 "2009-05-26T23:18:20Z")

</div>

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](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.
