From: Brian Candler Date: 2003-08-08T02:55:40+09:00 Subject: Re: Elegant solution for a loop-break problem On Fri, Aug 08, 2003 at 01:46:20AM +0900, Rudolf Polzer wrote: > he means, that in Perl you can do: > > use Data::Dumper; > @l = qw(one two three); > for my $elem(@l) > { > $elem = 'X' . uc $elem; > } > print Dumper \@l; > > which yields > > $VAR1 = [ > 'XONE', > 'XTWO', > 'XTHREE' > ]; In Ruby: l = %w{one two three} l.each do |elem| elem.replace('X' + elem.upcase) end p l In other words, you have to mutate the existing element; assigning to 'elem' does not replace the element, it simply makes the local variable point elsewhere. There is some Bad Magic going on in the Perl version; somehow $elem is aliasing an element of the array, it is not a normal variable at all. > Of course, elem.upcase!() would solve the problem... but in this case, > eventually the equivalent of Perl's map operator, Array#collect and > Array#collect!, are more appropiate: > > l = %w{one two three} > l.collect!() do |elem| > 'X' + elem.upcase() > end > p l Except the question was about hashes, and hashes don't have collect! (which is really an Array method) Regards, Brian.