Tim Hunter
2/12/2006 9:54:00 PM
Jeppe Jakobsen wrote:
> Hi, I want to square every element in my array using a block:
>
> a = (1..1000).to_a
> a.each {|x| x**2}
>
> But this does not seem to work, it just outputs my array completely
> unchanged :(
>
> --
> "winners never quit, quitters never win"
>
The collect method will produce a new array:
a.collect {|x| x*x}
or, if you want to modify the original array, use collect!