B
Boris Schmid
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>
"expected behaviour"
irb(main):005:0* array = [1,2]
=> [1, 2]
irb(main):006:0> array.map {|i| i + 3}
=> [4, 5]
irb(main):007:0> array
=> [1, 2]
irb(main):008:0>
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>
"expected behaviour"
irb(main):005:0* array = [1,2]
=> [1, 2]
irb(main):006:0> array.map {|i| i + 3}
=> [4, 5]
irb(main):007:0> array
=> [1, 2]
irb(main):008:0>