From: dblack@... Date: 2003-08-30T04:14:47+09:00 Subject: Re: #collect with block modifying receiver Hi -- On Sat, 30 Aug 2003, Hal Fulton wrote: > dblack@superlink.net wrote: > >>>I wouldn't like that, personally. I don't remember exactly where, but I > >>>believe that I have a piece of code taking advantage of the fact that a > >>>receiver *can* be modified during iteration. I don't modify during a > >>>#collect, but during an #each. One of the things you can do is: > >>> > >>> arr.each { |e| arr << f.flatten if f.kind_of?(Array) } > >> > >>Well, if I understand Matz correctly, this behavior is not guaranteed. > >> > >>If that's the case, you should change this to avoid being bitten > >>the way I was. > > > > > > I think (Matz - ?) that Matz was talking specifically about changing > > the length of the receiver during an iteration, since that raises the > > question of how many iterations there should be, what the next one > > should be if something gets sliced out, etc. > > > > Just modifying the objects during an iteration is different, I think. > > #map! does it, for example. Or: > > > > names.each {|name| name.upcase!} > > > > I'm pretty sure there's no danger with in-place things like this > > (as opposed to things that alter the meaning of the 'place' you're > > 'in' :-) > > Yes, I agree. But to avoid confusion for others, let me point out that > Austin's code *does* change the length of the receiver. Whoops -- too late to prevent confusion for me. My only excuse is that I was so focused on wondering what 'f' was that I lost track of what was actually happening :-) David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav