Hi --

On Sun, 6 Dec 2009, Raul Jara wrote:

> Under ruby 1.8.6, running
>
> [1, 2, 3].collect
>
> results in an array of [1, 2, 3].  In 1.9, however, running
>
> [1, 2, 3].collect
>
> results in an Enumerator, which I then have to call .to_a on.
> Alternately, I can supply a block
>
> [1, 2, 3].collect {|x| x }
>
> and end up with an array.  So I guess that means that the default block
> of {|x| x} was removed from the function.  I was just wondering if
> anyone had any insight into why that change was made, because it feels
> very counter intuitive to me.   I really liked the elegance of opening
> up a file and just calling collect to turn it into an array.

As others have said, there are better ways (mainly File.readlines). As
for map/collect in general: it's hard to come up with a useful value
when it's called without a block. I don't know of any use cases, in
fact, no matter what it returns. There was, briefly, a time in 1.9
when you could do:

   enum = array.enum_for(:map, &some_lambda)

and then run enumerable operations, pre-mapped so to speak, off of
enum. But you can't do that any more, so I think map without a block
is essentially useless. (I'd be interested if anyone knows of any
remaining use cases.)


David

-- 
THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
January 22-23, Tampa, Florida
Info and registration at http://www.thecompleatrubyist.com
--------------------------------------
My new job: http://tinyurl.com/yfpn9hz