[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array#each, safe when modifying it's elements?

Onion Knight

3/5/2007 3:11:00 AM

Will Array#each only go through each unique element once even if you
were to throw around the elements in the array or will it just take
whatever element is after the current one?
I tried it myself but it behaves a bit strange.

a = [1, 2, 3, 4, 5, 6, 7]
a.each do |x|
print x
if x == 2
a.reverse!
end
end

It prints 1254327. Somehow the last element was intact and printed a 7
instead of 1.

--
Posted via http://www.ruby-....

1 Answer

Onion Knight

3/5/2007 3:17:00 AM

0

No wait I'm dumb, there's a second 2 at the end so the array get
reversed again. I guess it proves it isn't intact though. To solve that
I'm thinking that I could just copy the array and use that copy for
iteration.

--
Posted via http://www.ruby-....