[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

reusing the result of previous iteration step for comparison

Tammo Tjarks

8/25/2007 3:25:00 PM

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