[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: ruby 1.9 and collect
From:       Ken Bloom <kbloom () gmail ! com>
Date:       2009-12-06 23:08:33
Message-ID: pan.2009.12.06.23.07.42 () gmail ! com
[Download RAW message or body]

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"]



-- 
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic