From: Mark Hubbart Date: 2004-07-18T09:04:05+09:00 Subject: Re: [NUBY] Ruby Way code for shuffling an array doesn't behave as expected On Jul 17, 2004, at 4:12 PM, Alexey Verkhovsky wrote: > On Sun, 2004-07-18 at 01:43, Mark Sparshatt wrote: >> It's because the block passed to collect is changing the contents of >> a, >> so it doesn't reach all the elements of a. > > So, Ruby iterators operate on the original collection and let you > modify > the collection in the process. Another fact useful to remember. > > This is, by the way, one thing I like about Java iterators: they are > fail-safe in such scenarios. E.g. > > List list = new ArrayList(2); > list.add(new Object()); > list.add(new Object()); > Iterator i = list.iterator(); > i.next(); // this is OK > list.add(new Object()); // list gets changed > i.next(); // throws ConcurrentModificationException > > IMHO, an exception here is The Right Thing (TM). At least, it saved me > from my own stupid mistakes more than once. I'm not sure that an exception would be The Right Thing, but I do wonder why it no longer works on a copy of the array? Allowing modification of the actual array as you iterate over it seems dangerous. But there must have been a reason for abandoning the original behavior... Perhaps it was a performance issue? cheers, Mark