# Reusing the result of previous iteration step for comparison

**URL:** <https://www.ruby-forum.com/t/reusing-the-result-of-previous-iteration-step-for-comparison/112995>\
**Category:** Ruby\
**Created:** [August 25, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/reusing-the-result-of-previous-iteration-step-for-comparison/112995 "2007-08-25T17:11:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [August 25, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/reusing-the-result-of-previous-iteration-step-for-comparison/112995/1 "2007-08-25T17:11:33Z")

</div>

I have the general the following kind of problem. I want to check  
somethin  
which has to hold for the whole collection. I need it for the selection  
comand.  
Simplified it is something like that

a = { “tst1” =\> [[1,0],[0,1]], “tst2” =\> [[0,0],[1,1]], “tst3” =\>  
[[0,0],[1,1]]}  
a.select {|elemkey,elemattr|  
equal = true  
elemattr.each {|v| equal = equal && (v[0]==v[1])}  
equal == true  
}

That works so far, but I think it is not a nice solution. Because I use  
the  
fact, that I use the variable equal which is defined outside the  
each-block.  
I am even unsure if that will work anymore with the next ruby-versions  
(1.9).  
Is there a solution only working with variables  
inside the each block? I would prefer a solution with:  
a = { “tst1” =\> [[1,0],[0,1]], “tst2” =\> [[0,0],[1,1]], “tst3” =\>  
[[0,0],[1,1]]}  
a.select {|elemkey,elemattr|  
equal = elemattr.each {|v| ???}  
}

Regards,  
Tammo

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/reusing-the-result-of-previous-iteration-step-for-comparison/112995/2 "2007-09-25T23:04:48Z")

</div>

Thank you very match,  
that helps.

Regards,  
Tammo
