# Does array's each-block destroies outer objects?

**URL:** <https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650>\
**Category:** Ruby\
**Created:** [September 27, 2008, 11:09am UTC](https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650 "2008-09-27T11:09:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [September 27, 2008, 11:09am UTC](https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650/1 "2008-09-27T11:09:25Z")

</div>

Hi,

Please look:

> x = 2840  
> =\> 2840  
> x.object\_id  
> =\> 5681

> a = [1,2,3,4,5]  
> =\> [1, 2, 3, 4, 5]  
> a[4].object\_id  
> =\> 11

> a.each {|x| puts x}  
> 1  
> 2  
> 3  
> 4  
> 5  
> =\> [1, 2, 3, 4, 5]

> a[4].object\_id  
> =\> 11

> x.object\_id  
> =\> 11

After execute the block , it looks like that ruby destroied the former  
‘x’ object, the ‘x’ object has object\_id 4 now, which is same as  
a[4].object\_id.

If so , does it mean that I must choose another variable name in  
array’s each block to keep the ‘x’ object alive ?

Thank you.

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [September 27, 2008, 11:15am UTC](https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650/2 "2008-09-27T11:15:51Z")

</div>

w wg wrote:

> After execute the block , it looks like that ruby destroied the former  
> ‘x’ object, the ‘x’ object has object\_id 4 now, which is same as  
> a[4].object\_id.

Yes, if you do {|foo|…} and you have a local variable foo outside the  
block,  
then the outer foo will be replaced by the value that is yielded to the  
block.  
This is a property of blocks in 1.8, not of each in particular. This  
behaviour  
will no longer be present in 1.9.

> If so , does it mean that I must choose another variable name in  
> array’s each block to keep the ‘x’ object alive ?

Yes.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [September 27, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650/3 "2008-09-27T12:01:46Z")

</div>

w wg wrote:

> After execute the block , it looks like that ruby destroied the former  
> ‘x’ object, the ‘x’ object has object\_id 4 now, which is same as  
> a[4].object\_id.

Hello. Have a look at this topic: [That was unexpected -- strange scoping behavior - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/166204)  
about the same problem.

TPR.

---

<div class="post-metadata">

**Author:** ![w\_wg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ba0ec/32.png) [@w\_wg](https://www.ruby-forum.com/u/w_wg)\
**Post date:** [September 27, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/does-arrays-each-block-destroies-outer-objects/149650/4 "2008-09-27T16:15:42Z")

</div>

On 27.09.2008 11:14, Sebastian H. wrote:

> w wg wrote:
> 
> > After execute the block , it looks like that ruby destroied the former  
> > ‘x’ object,

There is no object destroyed. The variable x is made to point to  
another object. If there are no more references to the object pointed  
to by x before it can be garbage collected at any point in time after x  
has been made to point to another instance.

> Yes.

Just to throw in the keyword for the concept involved to help in further  
research of the matter: the concept is called “scoping” - the scoping  
rules for a programming language determine which variables are visible  
in which program artifacts (blocks, methods…) and especially what  
happens to variables with identical names.

Kind regards

robert
