Shannon Fang wrote:
> Hi Matz,
>
>> Rejected.  Proposal Array#to_h(value=nil) does not actually solve the
>> problem above.  If you want to have _this_ to_h, you need to have
>> a proper usecase.
>
> I don't know what you mean the "problem above"... My purpose is to
> have a convenient way to convert array to hash for *indexing* or
> *fast searching* purpose.

If you just want a fast access to those elements a Set is sufficient - and
you can use #to_set already:

>> require 'set'
=> true
>> a=%w{foo bar baz}
=> ["foo", "bar", "baz"]
>> s=a.to_set
=> #<Set: {"baz", "foo", "bar"}>
>> s.include? "foo"
=> true
>> s.include? "fo"
=> false

> This indeed is a bit ad-hoc, but it is useful in may cases.

I think Matz just asked you to present these use cases.  Apart from your
general description I couldn't find one in the thread.  Did I miss
something?

> I
> deliberately wanted to map array value (not index) to hash key...
> otherwise I think it is not useful (i.e., map array key => hash key).

Problem is, that there are other conversions that are at least equally
reasonable (e.g. the one Nobu presented).  IMHO there is not a single
reasonable way to implement Array#to_h so it's better to leave it out.
Maybe it's just a naming issue though.

Kind regards

    robert