From: dblack@... Date: 2006-02-13T06:54:09+09:00 Subject: Re: newbie question about blocks Hi -- On Mon, 13 Feb 2006, 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 :( What you want is map, rather than each: a.map {|x| x**2 } each just returns the receiver (i.e., the array itself). map returns a new array, composed of the results obtained by running the block once for each item. "collect" is a synonym for map. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black