# Array to hash

**URL:** <https://www.ruby-forum.com/t/array-to-hash/89421>\
**Category:** Ruby\
**Created:** [March 20, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421 "2007-03-20T12:33:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/1 "2007-03-20T12:33:46Z")

</div>

Hello all

&nbsp; &nbsp; Is there a method to collect the items in an array into a hash

&nbsp;

I want to go from this X =[1,2,3,4,5,6] to this Y={1 =\>2, 3 =\>4, 5 =\>6}

&nbsp;

Just seems like there should be one

&nbsp;

Thanks in advance

&nbsp;

Sam

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/2 "2007-03-20T12:43:47Z")

</div>

Hi –

On 3/20/07, Servando G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all  
> Is there a method to collect the items in an array into a hash
> 
> I want to go from this X =[1,2,3,4,5,6] to this Y={1 =\>2, 3 =\>4, 5 =\>6}

Hash[\*X]

David

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/3 "2007-03-20T12:47:48Z")

</div>

On 3/20/07, Servando G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sam  
> Try this

arr = [1,2,3,4,5,6]  
ahash = Hash[\*arr]  
p arr  
p ahash

Harry

–

[http://www.kakueki.com/ruby/list.html](http://www.kakueki.com/ruby/list.html)  
Japanese Ruby List Subjects in English

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/4 "2007-03-20T12:53:49Z")

</div>

David 

&nbsp; &nbsp; No Way that is to easy. My new question to you is how does this work. Where can I read the source code for this method  
  

\> Hi --

\>

\> On 3/20/07, Servando Garcia wrote:

\>

\>\> Hello all

\>\> Is there a method to collect the items in an array into a hash

\>\>

\>\> I want to go from this X =[1,2,3,4,5,6] to this Y={1 =\>2, 3 =\>4,

\>\> 5 =\>6}

\>\>

\> Hash[\*X]

\>

\>

\> David

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/5 "2007-03-20T15:29:58Z")

</div>

On 20 mars 07, at 12:53, Servando G. wrote:

> Where can I read the source code for this method ?

\<[http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/hash.c](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/hash.c)?  
revision=11708&view=markup\>

The function you are looking for is ‘rb\_hash\_s\_create’.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/6 "2007-03-20T15:37:20Z")

</div>

Hi –

On 3/20/07, Servando G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> David  
> No Way that is to easy. My new question to you is how does this work.

The ‘unarray’ operator \* turns an array into a list. So in effect  
you’re doing:

Hash[1,2,3,4,5,6]

David

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/7 "2007-03-20T12:51:48Z")

</div>

Guess I should have googled harder/longer I have found some code that appears to work. Now I'll have to spend sometime stepping through it.

Here is the code:

&nbsp;

class Array  
def to\_h(&block)  
Hash[\*self.collect { |v|  
[v, block.call(v)]  
}.flatten]  
end  
end

&nbsp;

&nbsp;

and the link to the page:

[http://snippets.dzone.com/posts/show/302](http://snippets.dzone.com/posts/show/302)

&nbsp;

Harold, Please forgive my impatience

&nbsp;

Sam  
  

\> Hello all

\> &nbsp; Is there a method to collect the items in an array into a hash

\>

\> I want to go from this X =[1,2,3,4,5,6] to this Y={1 =\>2, 3 =\>4, 5

\> =\>6}

\>

\> Just seems like there should be one

\>

\> Thanks in advance

\>

\> Sam

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 9:32pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/8 "2007-03-20T21:32:43Z")

</div>

On Tue, Mar 20, 2007 at 08:33:09PM +0900, Servando G. wrote:

> I want to go from this X =[1,2,3,4,5,6] to this Y={1 =\>2, 3 =\>4, 5 =\>6}
> 
> &nbsp;
> 
> Just seems like there should be one
> 
> &nbsp;
> 
> Thanks in advance
> 
> &nbsp;
> 
> Sam

I’m glad you got the answer you were seeking. I have a request, though:

Please provide plain text email content in the future. Some of us  
(intentionally) avoid HTML email.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 20, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/array-to-hash/89421/9 "2007-03-20T15:40:21Z")

</div>

On 3/20/07, Servando G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> David  
> No Way that is to easy. My new question to you is how does this work.  
> Where can I read the source code for this method

It’s a combination of two things:

1. Hash.[] creates a hash from a comma separated even-length list

Hash[1,2,3,4,5,6] # =\> { 1=\>2, 3=\>4, 5=\>6 }

1. \*ary converts an array into a comma-separated list

def foo(a, b=nil, c=nil)  
p a  
p b  
p c  
end

ary = [1,2,3]

def foo(a, b=nil, c=nil)  
p a  
p b  
p c  
end

ary = [1,2,3]

irb\> foo(ary)  
[1, 2, 3]  
nil  
nil  
=\> nil  
irb\> foo(\*ary)  
1  
2  
3  
=\> nil

martin
