On Sunday, July 20, 2003, 1:31:42 AM, Yukihiro wrote:

> Hi,

> In message "Proposal: Array#to_h, to simplify hash generation"
>     on 03/07/19, Gavin Sinclair <gsinclair / soyabean.com.au> writes:

> |If Ruby had Array#to_h, then I could rewrite it as:
> |
> |    # return { filename -> size }
> |  def get_local_gz_files
> |    Dir["*.gz"].map { |filename|
> |      [ filename, File.stat(filename).size ]
> |    }.to_h
> |  end

> It has been proposed several times.

I thought it sounded familiar, but didn't see an RCR.

> The issues are

>   * whether the name "to_h" is a good name or not.  somebody came up
>     with the name "hashify".  I'm not excited by both names.

#to_h sounds good to me - we already have to_s, to_a, to_i, etc.  It's
just too sweet that Hash#to_a and Array#to_h should be the inverse of
each other.

What don't you like about #to_h?

#to_hash is fine by me too, but I don't really know the nuances of
to_s/to_str, to_a/to_ary, ...

>   * what if the original array is not an assoc array (array of arrays
>     of two elements).  raise error? ignore?

Raise error.  #to_h is clearly a method to be used with care.  People
are unlikely to call it on random objects.  Of course, [1,2,3,4].to_h
could be the equivalent to Hash[1,2,3,4].  But then there's the corner
case: [ [1,2], "x", [7,8], "g" ].to_h.

I think I would insist on the input being an assoc array.

Gavin