Hi --

On Mon, 7 Dec 2009, Ken Bloom wrote:

> On Sun, 06 Dec 2009 22:09:53 +0900, David A. Black wrote:
>
>> Hi --
>>
>> On Sun, 6 Dec 2009, Martin DeMello wrote:
>>
>>> On Sun, Dec 6, 2009 at 2:34 PM, David A. Black <dblack / rubypal.com>
>>> wrote:
>>>>
>>>> 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.
>>>
>>> The use case I can see is to turn an Enumerable into an Array. Which
>>> means that you should be using to_a instead, and checking,
>>> File.open("foo").to_a does indeed do the right thing.
>>
>> It will work (using map instead of to_a in 1.8), but I don't think it's
>> a real use case, in the sense that it would never be the best way. In
>> other words, there's a use case for what map without a block does, but
>> not for using map to do it.
>>
>> I'm not sure there are any real uses for map returning an enumerator, as
>> in 1.9, either. You can do:
>>
>>    a = [1,2,3]
>>    e = a.map
>>    e.each {|x| x * 10 }  # [10, 20, 30]
>>
>> but that's just a long way of writing map.
>>
>>
>> David
>
> ["foo","bar","baz"].map.each_with_index{|val,idx| "#{idx} #{val}"}
> => ["0 foo","1 bar","2 baz"]

Sort of an ironic example, in that I spent nine years lobbying for
map_with_index :-) You can do it this way though:

   array.map.with_index {|e,i| ... }

with_index being one of the few methods that Enumerator has that
aren't from Enumerable.

My main problem with all this is the fact that you can't do this any
more:

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

but I guess the mourning period for that should be officially declared
over :-)


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