From: Alex Young Date: 2007-12-13T21:51:04+09:00 Subject: Re: suprised by destructive map (not map!) Boris Schmid wrote: > Occasionally I run into bits of ruby that are surprising to me (although > might not be to matz). Here is one of them. Should I file this as a bug > (I thought .map is supposed to be non-destructive), or just keep in mind > which classes are passed by value and which are passed by reference. > > Basically: map (and naturally also map!) is destructive when your array > includes elements which are normally not passed by value but by > reference. > > ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux] > boris:falcon:~:irb > > "unexpected (for me)" > irb(main):001:0> array = [[1],[2]] > => [[1], [2]] > irb(main):002:0> array.map {|i| i << 3} > => [[1, 3], [2, 3]] > irb(main):003:0> array > => [[1, 3], [2, 3]] > irb(main):004:0> It's not map that's destructive here, it's the #<< method. Map iterates over the original array, and creates a new one with the results of the block - it doesn't copy the original array first. -- Alex