Hi --

On Sat, 7 Feb 2009, Julian Leviston wrote:

> On 01/02/2009, at 7:03 AM, David A. Black wrote:
>
>> Hi --
>> 
>> On Sun, 1 Feb 2009, Roger Pack wrote:
>> 
>>> Not that I would find it useful at all, but is there is a Hash#to_a
>>> should there not be an Array#to_h?
>> 
>> Not if it's not useful :-) It might be, though. It's been talked about
>> a lot over the years. As I recall, part of the problem is the question
>> of what it would mean; for example, given this:
>>
>>  ["a","b","c","d"].to_h
>> 
>> is it
>>
>>  ["a" => "b", "c" => "d"]
>> 
>> or
>>
>>  [0 => "a", 1 => "b", ....]
>> 
>> ?
>> 
> It's fairly trivial, isn't it?
>
>>> class Array
>>> def to_h
>>> Hash[*self]
>>> end
>>> end
> => nil
>>> [1,2,3,4].to_h
> => {1=>2, 3=>4}

Yes, if that's the semantics you want. If you're inclined toward
thinking it should use the array indices as hash keys and the array
values as hash values, then that implementation wouldn't work. My
point was that there's been debate about the semantics -- i.e., what
Array#to_h should actually do.


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!