# RCR: Array#to\_h

**URL:** <https://rubytalk.org/t/rcr-array-to-h/21856>\
**Category:** ruby-talk\
**Created:** [25 October 2005 03:10 UTC](https://rubytalk.org/t/rcr-array-to-h/21856 "2005-10-25T03:10:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 03:10 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/1 "2005-10-25T03:10:18Z")

</div>

Hi there,

I have registered 2 accounts for RCRkive, but all failed... So I posted here.

Abstract

A simple method that construct a hash from an array. Just like Hash#to\_a return an array from hash.

Problem

Consider this cenario: I programmed a multi-threaded web page downloader. One of its input is an array of urls to download. In the program, I wanted to use a hash like {'[http://ruby-lang.org/index.html](http://ruby-lang.org/index.html)' =\> '200'}, i.e., use a hash to record the return code of the http request. This way I can avoid re-download a page, or miss a page.

For the user of this download program, it is much easier to use an Array instead of a Hash:

d = WebPageDownloader.new  
d.links = IO.readlines('list.txt')

Hence, in the program we want to do:

@links = @links.to\_h

Proposal

Add a method to\_h (not to\_hash) in the Array class, so that user can:

a = [1, 2, 3, 4, 5]  
p a.to\_h { |i, v| v \* 2} --\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}  
p a.to\_h(3) --\> {5=\>3, 1=\>3, 2=\>3, 3=\>3, 4=\>3}

Anaysis

This is very convenient for users who need this feature, and it will not affect behavior of the Array class in anyway for those do not need this feature.

Please refer to RCR278, which is similar but not same.

Implementation

class Array  
&nbsp;&nbsp;public  
&nbsp;&nbsp;def to\_h(value = nil)  
&nbsp;&nbsp;&nbsp;&nbsp;\_hash = {}  
&nbsp;&nbsp;&nbsp;&nbsp;self.each\_index do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = self[i]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given? then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_hash[v] = yield(i, v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_hash[v] = value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;\_hash  
&nbsp;&nbsp;end  
end

Please comment.

Thanks,  
Shannon

---

<div class="post-metadata">

**Author:** ![Marcel\_Molina\_Jr](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Marcel\_Molina\_Jr](https://rubytalk.org/u/Marcel_Molina_Jr)\
**Post date:** [25 October 2005 03:16 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/2 "2005-10-25T03:16:04Z")

</div>

> Abstract
> 
> A simple method that construct a hash from an array. Just like Hash#to\_a  
> return an array from hash.

Having your proposed to\_h accept a block is a nice idea but as it is you can  
do the non-block version quite easily:

> > Hash[\*%w(a b c d)]

=\> {"a"=\>"b", "c"=\>"d"}

> > keys = %w(a b c d)

=\> ["a", "b", "c", "d"]

> > values = [1, 2, 3, 4]

=\> [1, 2, 3, 4]

> > Hash[\*keys.zip(values).flatten]

=\> {"a"=\>1, "b"=\>2, "c"=\>3, "d"=\>4}

marcel

> **···**
>
> On Tue, Oct 25, 2005 at 12:10:18PM +0900, Shannon Fang wrote:  
> --  
> Marcel Molina Jr. \<marcel@vernix.org\>

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [25 October 2005 03:48 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/3 "2005-10-25T03:48:40Z")

</div>

Hi,

At Tue, 25 Oct 2005 12:10:18 +0900,  
Shannon Fang wrote in [ruby-talk:162399]:

> Abstract
> 
> A simple method that construct a hash from an array. Just like Hash#to\_a  
> return an array from hash.

Agreed here, but

> Proposal
> 
> Add a method to\_h (not to\_hash) in the Array class, so that user can:
> 
> a = [1, 2, 3, 4, 5]  
> p a.to\_h { |i, v| v \* 2} --\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}  
> p a.to\_h(3) --\> {5=\>3, 1=\>3, 2=\>3, 3=\>3, 4=\>3}

I expect that method as:

&nbsp;&nbsp;h = {"foo"=\>1,"bar"=\>2}  
&nbsp;&nbsp;a = h.to\_a # =\> [["foo", 1], ["bar", 2]]  
&nbsp;&nbsp;a.to\_h # =\> {"foo"=\>1,"bar"=\>2} == h

&nbsp;&nbsp;module Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;def to\_h  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each {|key, value| hash[key] = value}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 October 2005 06:17 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/4 "2005-10-25T06:17:00Z")

</div>

Hi,

> **···**
>
> In message "Re: RCR: Array#to\_h" on Tue, 25 Oct 2005 12:10:18 +0900, "Shannon Fang" \<xrfang@hotmail.com\> writes:
> 
> > Abstract
> > 
> > A simple method that construct a hash from an array. Just like Hash#to\_a  
> > return an array from hash.
> > 
> > Problem
> > 
> > Consider this cenario: I programmed a multi-threaded web page downloader.  
> > One of its input is an array of urls to download. In the program, I wanted  
> > to use a hash like {'[http://ruby-lang.org/index.html&#39](http://ruby-lang.org/index.html&#39); =\> '200'}, i.e., use a  
> > hash to record the return code of the http request. This way I can avoid  
> > re-download a page, or miss a page.
> > 
> > For the user of this download program, it is much easier to use an Array  
> > instead of a Hash:
> > 
> > d = WebPageDownloader.new  
> > d.links = IO.readlines('list.txt')
> > 
> > Hence, in the program we want to do:
> > 
> > @links = @links.to\_h
> > 
> > Proposal
> > 
> > Add a method to\_h (not to\_hash) in the Array class, so that user can:
> > 
> > a = [1, 2, 3, 4, 5]  
> > p a.to\_h { |i, v| v \* 2} --\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}  
> > p a.to\_h(3) --\> {5=\>3, 1=\>3, 2=\>3, 3=\>3, 4=\>3}
> 
> 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.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 October 2005 10:16 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/5 "2005-10-25T10:16:22Z")

</div>

Hi --

> Hi there,
> 
> I have registered 2 accounts for RCRkive, but all failed... So I posted here.

You'll have to give me more details if you want it fixed 🙂

> Abstract
> 
> A simple method that construct a hash from an array. Just like Hash#to\_a return an array from hash.

See [RCRS](http://www.rcrchive.net/rejected.html#rcr12)

David

> **···**
>
> On Tue, 25 Oct 2005, Shannon Fang wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [25 October 2005 04:37 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/6 "2005-10-25T04:37:01Z")

</div>

I think the proposal is over specialized. Why should the array elements  
neccessarily become keys?

Nobu, I agree except 1) key,value in Enumerable? 2) other clearly  
polymorphic solution:

module Enumerable  
&nbsp;&nbsp;def to\_h  
&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index {|value,index| hash[i] = value}  
&nbsp;&nbsp;&nbsp;&nbsp;hash  
&nbsp;&nbsp;end  
end

Hence hash key \<-\> array index. And it is by definition "Enumerable".

T.

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 04:38 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/7 "2005-10-25T04:38:06Z")

</div>

The block may be useful because you may want to, for example, do some calculation, or even lookup the database...

Shannon

> **···**
>
> > From: "Marcel Molina Jr." \<marcel@vernix.org\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 12:16:04 +0900
> > 
> > On Tue, Oct 25, 2005 at 12:10:18PM +0900, Shannon Fang wrote:  
> > \> Abstract  
> > \>  
> > \> A simple method that construct a hash from an array. Just like Hash#to\_a  
> > \> return an array from hash.
> > 
> > Having your proposed to\_h accept a block is a nice idea but as it is you can  
> > do the non-block version quite easily:
> > 
> > \>\> Hash[\*%w(a b c d)]  
> > =\> {"a"=\>"b", "c"=\>"d"}  
> > \>\> keys = %w(a b c d)  
> > =\> ["a", "b", "c", "d"]  
> > \>\> values = [1, 2, 3, 4]  
> > =\> [1, 2, 3, 4]  
> > \>\> Hash[\*keys.zip(values).flatten]  
> > =\> {"a"=\>1, "b"=\>2, "c"=\>3, "d"=\>4}
> > 
> > marcel  
> > --  
> > Marcel Molina Jr. \<marcel@vernix.org\>

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 04:46 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/8 "2005-10-25T04:46:33Z")

</div>

Ok, we may consider Array#to\_h as a true reverse operation of Hash#to\_a, however, I think the feature I proposed has different usage. May be anyone has a better name for easy understanding?

Also, I am not very clear about this code:

> &nbsp;&nbsp;module Enumerable  
> &nbsp;&nbsp;&nbsp;&nbsp;def to\_h  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each {|key, value| hash[key] = value}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

Array mixed Eumerable, hence, if use the above code, we have:

arr = [a, b, c, d, e]  
arr.to\_h =\> {1 =\> a, 2 =\> b, 3 =\> c, 4 =\> d, 5 =\>e}

Which seems not very useful, I think the to\_h operation of array should use array VALUE as the hash KEY.

Shannon

> **···**
>
> > From: nobuyoshi nakada \<nobuyoshi.nakada@ge.com\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 12:48:40 +0900
> > 
> > Hi,
> > 
> > At Tue, 25 Oct 2005 12:10:18 +0900,  
> > Shannon Fang wrote in [ruby-talk:162399]:  
> > \> Abstract  
> > \>  
> > \> A simple method that construct a hash from an array. Just like Hash#to\_a  
> > \> return an array from hash.
> > 
> > Agreed here, but
> > 
> > \> Proposal  
> > \>  
> > \> Add a method to\_h (not to\_hash) in the Array class, so that user can:  
> > \>  
> > \> a = [1, 2, 3, 4, 5]  
> > \> p a.to\_h { |i, v| v \* 2} --\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}  
> > \> p a.to\_h(3) --\> {5=\>3, 1=\>3, 2=\>3, 3=\>3, 4=\>3}
> > 
> > I expect that method as:
> > 
> > &nbsp;&nbsp;h = {"foo"=\>1,"bar"=\>2}  
> > &nbsp;&nbsp;a = h.to\_a # =\> [["foo", 1], ["bar", 2]]  
> > &nbsp;&nbsp;a.to\_h # =\> {"foo"=\>1,"bar"=\>2} == h
> > 
> > &nbsp;&nbsp;module Enumerable  
> > &nbsp;&nbsp;&nbsp;&nbsp;def to\_h  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each {|key, value| hash[key] = value}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > --  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 07:02 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/9 "2005-10-25T07:02:06Z")

</div>

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.

This indeed is a bit ad-hoc, but it is useful in may cases. 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).

I think there might be 2 types of RCRs, one is more "scientific", those deep into the design of language. Another is more "engineering", those add a method/feature to a single class for convinience, like Array#nitems.

I think for the latter one, the criteria to accept/reject, is if it is useful for "lots of" Ruby programmers..., if that's your criteria, then I think I will wait to see if others want this 😃  
else, if one of your criterion is to make Ruby "pure", all those things should belong to a library not the language itself (or core classes), then pls also let me know, so that I can have a better idea when propose RCR in the future.

Thanks,  
Shannon

> **···**
>
> > From: Yukihiro Matsumoto \<matz@ruby-lang.org\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 15:17:00 +0900
> > 
> > Hi,
> > 
> > In message "Re: RCR: Array#to\_h" \> on Tue, 25 Oct 2005 12:10:18 +0900, "Shannon Fang" \>\<xrfang@hotmail.com\> writes:
> > 
> > > Abstract
> > > 
> > > A simple method that construct a hash from an array. Just like Hash#to\_a  
> > > return an array from hash.
> > > 
> > > Problem
> > > 
> > > Consider this cenario: I programmed a multi-threaded web page downloader.  
> > > One of its input is an array of urls to download. In the program, I wanted  
> > > to use a hash like {'[http://ruby-lang.org/index.html&#39](http://ruby-lang.org/index.html&#39); =\> '200'}, i.e., use
> > 
> > a
> > 
> > > hash to record the return code of the http request. This way I can avoid  
> > > re-download a page, or miss a page.
> > > 
> > > For the user of this download program, it is much easier to use an Array  
> > > instead of a Hash:
> > > 
> > > d = WebPageDownloader.new  
> > > d.links = IO.readlines('list.txt')
> > > 
> > > Hence, in the program we want to do:
> > > 
> > > @links = @links.to\_h
> > > 
> > > Proposal
> > > 
> > > Add a method to\_h (not to\_hash) in the Array class, so that user can:
> > > 
> > > a = [1, 2, 3, 4, 5]  
> > > p a.to\_h { |i, v| v \* 2} --\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}  
> > > p a.to\_h(3) --\> {5=\>3, 1=\>3, 2=\>3, 3=\>3, 4=\>3}
> > 
> > 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.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 11:52 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/10 "2005-10-25T11:52:02Z")

</div>

Hi David,

I have registered RCR account using xrfang@hotmail.com and sales@spamweed.com. Please tell me when I can use one of these 🙂

Thank you!  
Shannon

> **···**
>
> > From: "David A. Black" \<dblack@wobblini.net\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 19:16:22 +0900
> > 
> > Hi --
> > 
> > On Tue, 25 Oct 2005, Shannon Fang wrote:
> > 
> > > Hi there,
> > > 
> > > I have registered 2 accounts for RCRkive, but all failed... So I posted here.
> > 
> > You'll have to give me more details if you want it fixed 🙂
> > 
> > > Abstract
> > > 
> > > A simple method that construct a hash from an array. Just like Hash#to\_a return an array from hash.
> > 
> > See [RCRS](http://www.rcrchive.net/rejected.html#rcr12)
> > 
> > David
> > 
> > --  
> > David A. Black  
> > dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Steven\_Lumos1](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Steven\_Lumos1](https://rubytalk.org/u/Steven_Lumos1)\
**Post date:** [26 October 2005 04:23 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/11 "2005-10-26T04:23:42Z")

</div>

"Marcel Molina Jr." \<marcel@vernix.org\> writes:

> > Abstract
> > 
> > A simple method that construct a hash from an array. Just like Hash#to\_a  
> > return an array from hash.
> 
> Having your proposed to\_h accept a block is a nice idea but as it is you can  
> do the non-block version quite easily:
> 
> > > Hash[\*%w(a b c d)]
> 
> =\> {"a"=\>"b", "c"=\>"d"}
> 
> > > keys = %w(a b c d)
> 
> =\> ["a", "b", "c", "d"]
> 
> > > values = [1, 2, 3, 4]
> 
> =\> [1, 2, 3, 4]
> 
> > > Hash[\*keys.zip(values).flatten]
> 
> =\> {"a"=\>1, "b"=\>2, "c"=\>3, "d"=\>4}

And the block version:

> > a = [1,2,3,4,5]

=\> [1, 2, 3, 4, 5]

> > Hash[\*a.map {|e| [e, 2\*e]}.flatten]

=\> {5=\>10, 1=\>2, 2=\>4, 3=\>6, 4=\>8}

Steve

> **···**
>
> > On Tue, Oct 25, 2005 at 12:10:18PM +0900, Shannon Fang wrote:

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 04:52 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/12 "2005-10-25T04:52:07Z")

</div>

> I think the proposal is over specialized. Why should the array elements  
> neccessarily become keys?

This is actually the useful feature! I already said not everybody need it... However, I don't see any need to map array key to hash key:

a = [1, 2, 3, 4, 5]  
b = {1, 2, 3, 4, 5}

a[0] = 1  
b[0] = 1

why we need to convert a to b?? The reason to use a hash is for fast access, not by using sequential index!

Shannon

> **···**
>
> > Nobu, I agree except 1) key,value in Enumerable? 2) other clearly  
> > polymorphic solution:
> > 
> > module Enumerable  
> > &nbsp;&nbsp;def to\_h  
> > &nbsp;&nbsp;&nbsp;&nbsp;hash = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index {|value,index| hash[i] = value}  
> > &nbsp;&nbsp;&nbsp;&nbsp;hash  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Hence hash key \<-\> array index. And it is by definition "Enumerable".
> > 
> > T.

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [25 October 2005 04:57 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/13 "2005-10-25T04:57:02Z")

</div>

Actually this has been bugging me as I currently have these:

module Enumerable

&nbsp;&nbsp;# Produces a hash from an Enumerable with index for keys.

> **···**
>
> #  
> &nbsp;&nbsp;# a1 = [:a, :b]  
> &nbsp;&nbsp;# a1.to\_h #=\> { 0=\>:a, 1=\>:b }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;def to\_h( &blk )  
> &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = blk.call(e,i) }  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = e }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;end
> 
> end
> 
> class Array
> 
> &nbsp;&nbsp;# Produces a hash for an Array, or two arrays.  
> &nbsp;&nbsp;# It is just like Enumerbale#to\_h but with an  
> &nbsp;&nbsp;# extra feature: If an array is given as the  
> &nbsp;&nbsp;# values, it is zipped with the receiver,  
> &nbsp;&nbsp;# to produce the hash.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a1 = [:a, :b]  
> &nbsp;&nbsp;# a2 = [1, 2]  
> &nbsp;&nbsp;# a1.to\_h(a2) #=\> { :a=\>1, :b=\>2 }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;def to\_h(values=nil)  
> &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;if values  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size.times{ |i| h[at(i)] = [values.at](http://values.at)(i) }  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = e }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# Converts an associative array into a hash.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1], [:b,2] ]  
> &nbsp;&nbsp;# a.assoc\_to\_h #=\> { :a=\>1, :b=\>2 }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1,2], [:b,3,4] ]  
> &nbsp;&nbsp;# a.assoc\_to\_h(true) #=\> { :a=\>[1,2], :b=\>[3,4] }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;def assoc\_to\_h(arrayed=nil)  
> &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;if arrayed  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each{ |e| h[e.first] = e.slice(1..-1) }  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each{ |e| h[e.first] = e.last }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;end
> 
> end
> 
> I wish there was a good way just to have a single Array#to\_h, but it  
> doesn;t seem reasonable. Perhaps Enumerable#to\_h could be #to\_hash?
> 
> T.

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [25 October 2005 05:39 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/14 "2005-10-25T05:39:20Z")

</div>

Hi,

At Tue, 25 Oct 2005 13:37:01 +0900,  
Trans wrote in [ruby-talk:162411]:

> I think the proposal is over specialized. Why should the array elements  
> neccessarily become keys?

Yes, agreed, and I doubt that there is a solution which  
satisfy everyone for this issue.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [25 October 2005 07:32 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/15 "2005-10-25T07:32:02Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 October 2005 10:13 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/16 "2005-10-25T10:13:14Z")

</div>

Hi --

> **···**
>
> On Tue, 25 Oct 2005, Trans wrote:
> 
> > I think the proposal is over specialized. Why should the array elements  
> > neccessarily become keys?
> > 
> > Nobu, I agree except 1) key,value in Enumerable? 2) other clearly  
> > polymorphic solution:
> > 
> > module Enumerable  
> > def to\_h  
> > &nbsp;&nbsp;&nbsp;hash = {}  
> > &nbsp;&nbsp;&nbsp;each\_with\_index {|value,index| hash[i] = value}  
> > &nbsp;&nbsp;&nbsp;hash  
> > end  
> > end
> > 
> > Hence hash key \<-\> array index. And it is by definition "Enumerable".
> 
> But then what's the point? 🙂
> 
> This raises the old question of why hashes have both keys and  
> numerical indices. I've always maintained that they shouldn't.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 05:05 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/17 "2005-10-25T05:05:07Z")

</div>

I am just reading the PickAxe 2e recently... According to Dave, to\_h means a representation of the object in hash, however to\_hash means this object is inherently compatible with hash, like to\_i and to\_int...

We do need a good name for it 😃

Shannon

> **···**
>
> > From: "Trans" \<transfire@gmail.com\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 13:57:02 +0900
> > 
> > Actually this has been bugging me as I currently have these:
> > 
> > module Enumerable
> > 
> > &nbsp;&nbsp;# Produces a hash from an Enumerable with index for keys.  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;# a1 = [:a, :b]  
> > &nbsp;&nbsp;# a1.to\_h #=\> { 0=\>:a, 1=\>:b }  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;def to\_h( &blk )  
> > &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = blk.call(e,i) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = e }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;h  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > class Array
> > 
> > &nbsp;&nbsp;# Produces a hash for an Array, or two arrays.  
> > &nbsp;&nbsp;# It is just like Enumerbale#to\_h but with an  
> > &nbsp;&nbsp;# extra feature: If an array is given as the  
> > &nbsp;&nbsp;# values, it is zipped with the receiver,  
> > &nbsp;&nbsp;# to produce the hash.  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;# a1 = [:a, :b]  
> > &nbsp;&nbsp;# a2 = [1, 2]  
> > &nbsp;&nbsp;# a1.to\_h(a2) #=\> { :a=\>1, :b=\>2 }  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;def to\_h(values=nil)  
> > &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;if values  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size.times{ |i| h[at(i)] = values.at(i) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index{ |e,i| h[i] = e }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;h  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;# Converts an associative array into a hash.  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;# a = [[:a,1], [:b,2] ]  
> > &nbsp;&nbsp;# a.assoc\_to\_h #=\> { :a=\>1, :b=\>2 }  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;# a = [[:a,1,2], [:b,3,4] ]  
> > &nbsp;&nbsp;# a.assoc\_to\_h(true) #=\> { :a=\>[1,2], :b=\>[3,4] }  
> > &nbsp;&nbsp;#  
> > &nbsp;&nbsp;def assoc\_to\_h(arrayed=nil)  
> > &nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> > &nbsp;&nbsp;&nbsp;&nbsp;if arrayed  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each{ |e| h[e.first] = e.slice(1..-1) }  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each{ |e| h[e.first] = e.last }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;h  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > I wish there was a good way just to have a single Array#to\_h, but it  
> > doesn;t seem reasonable. Perhaps Enumerable#to\_h could be #to\_hash?
> > 
> > T.

---

<div class="post-metadata">

**Author:** ![Shannon\_Fang](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shannon\_Fang](https://rubytalk.org/u/Shannon_Fang)\
**Post date:** [25 October 2005 08:16 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/18 "2005-10-25T08:16:22Z")

</div>

Hi Robert,

1) Set won't work. I am not only test if an element exist or not, I want to assign value to the key to track its status.

2) I have not write any RCR before, so I may make mistakes. I will certainly propose a good use case, when I have a complete one.

3) \>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.

Yes I agree it is a name issue.

Thanks,  
Shannon

> **···**
>
> > From: "Robert Klemme" \<bob.news@gmx.net\>  
> > Reply-To: ruby-talk@ruby-lang.org  
> > To: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Subject: Re: RCR: Array#to\_h  
> > Date: Tue, 25 Oct 2005 16:32:02 +0900
> > 
> > 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
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [25 October 2005 12:52 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/19 "2005-10-25T12:52:02Z")

</div>

David A. Black wrote:

> Hi --
> 
> \> I think the proposal is over specialized. Why should the array elements  
> \> neccessarily become keys?  
> \>  
> \> Nobu, I agree except 1) key,value in Enumerable? 2) other clearly  
> \> polymorphic solution:  
> \>  
> \> module Enumerable  
> \> def to\_h  
> \> hash = {}  
> \> each\_with\_index {|value,index| hash[i] = value}  
> \> hash  
> \> end  
> \> end  
> \>  
> \> Hence hash key \<-\> array index. And it is by definition "Enumerable".
> 
> But then what's the point? 🙂

Perhaps little. But you may simply need a hash based off an enumerable  
and this could be one step it getting it:

&nbsp;&nbsp;('a'..'c').to\_h.invert #=\> { 'a'=\>1, 'b'=\>2, 'c'=\>3 }

And of course it's consistant with what Enumerable is.

> This raises the old question of why hashes have both keys and  
> numerical indices. I've always maintained that they shouldn't.

Right. Hash doesn't really have index, it's a fake --a counter only. As  
you'll recall, we've talked about this before, and if I recall  
correctly, we even came to a shared conclusion, which is something I've  
been meaning to ask Matz:

have you given anymore consideration to deprecating  
Enumerabl#each\_with\_index in favor of Enumerable#each\_with\_counter;  
Array would retain it's own #each\_with\_index.

T.

> **···**
>
> > On Tue, 25 Oct 2005, Trans wrote:

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [25 October 2005 06:02 UTC](https://rubytalk.org/t/rcr-array-to-h/21856/20 "2005-10-25T06:02:02Z")

</div>

BTW have you tried:

module Enumerable

&nbsp;&nbsp;# Like \<tt\>#map\</tt\>/\<tt\>#collect\</tt\>, but it generates a Hash. The  
block  
&nbsp;&nbsp;# is expected to return two values: the key and the value for the new  
hash.

> **···**
>
> #  
> &nbsp;&nbsp;# numbers = (1..3)  
> &nbsp;&nbsp;# squares = numbers.graph { |n| [n, n\*n] } # { 1=\>1, 2=\>4, 3=\>9  
> }  
> &nbsp;&nbsp;# sq\_roots = numbers.graph { |n| [n\*n, n] } # { 1=\>1, 4=\>2, 9=\>3  
> }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;#--  
> &nbsp;&nbsp;# Credit for original version goes to Zallus Kanite and Gavin  
> Sinclair.  
> &nbsp;&nbsp;#++  
> &nbsp;&nbsp;def graph(&yld)  
> &nbsp;&nbsp;&nbsp;&nbsp;if yld  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject({}) do |h,kv|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nk, nv = yld[\*kv]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[nk] = nv  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hash[\*self.to\_a.flatten]  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> end

[Next page](https://rubytalk.org/t/rcr-array-to-h/21856.md?page=2)
