# Ruby Array to Hash (a better way?)

**URL:** <https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988>\
**Category:** Ruby\
**Created:** [August 1, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988 "2007-08-01T19:32:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/1 "2007-08-01T19:32:00Z")

</div>

Hey Guys,

I want to convert an array like the following to a hash. Below is my  
current code (I am assuming there is a better way).

Thanks

[“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”] convert to  
{“foo”=\>“bar”, “foo1”=\>“bar1”,“foo2”=\>“bar2”}

My Current Code:

def array\_to\_hash(array)  
count = 0  
hash = Hash.new  
(array.length / 2).times do  
hash[array[count]] = array[count+1]  
count += 2  
end  
return hash  
end

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/2 "2007-08-01T19:36:01Z")

</div>

skibud2 pisze:

> Hey Guys,
> 
> I want to convert an array like the following to a hash. Below is my  
> current code (I am assuming there is a better way).
> 
> Thanks
> 
> [“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”] convert to  
> {“foo”=\>“bar”, “foo1”=\>“bar1”,“foo2”=\>“bar2”}

given:  
a = [“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”]

puts Hash[\*a]

or:

require ‘enumerator’  
h = {}  
a.each\_slice(2){|k,v|h[k]=v}  
puts h

lopex

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/3 "2007-08-01T19:38:01Z")

</div>

On Aug 1, 12:26 pm, skibud2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey Guys,
> 
> I want to convert an array like the following to a hash. Below is my  
> current code (I am assuming there is a better way).
> 
> Thanks
> 
> [“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”] convert to  
> {“foo”=\>“bar”, “foo1”=\>“bar1”,“foo2”=\>“bar2”}

Use Hash#[]

> > Hash[“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”]  
> > =\> {“foo1”=\>“bar1”, “foo2”=\>“bar2”, “foo”=\>“bar”}

hth

Gordon

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/4 "2007-08-01T21:10:14Z")

</div>

I’ve been using Ruby for awhile, and I’m afraid I’ve never used the \*  
notation…

I can see that Hash[a] wouldn’t work, and that Hash[\*a] expands the  
array out so that Hash can use it elegantly.

But what, in a general, does the \* do?

Or is it just for special cases like this?

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/5 "2007-08-01T21:14:15Z")

</div>

Kyle S. wrote:

> But what, in a general, does the \* do?

It turns an array into a list of parameters (or, if used in a method  
definition, a list of parameters into an array).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 2, 2007, 12:01am UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/6 "2007-08-02T00:01:32Z")

</div>

On 01.08.2007 19:34, Marcin MielÅ¼yÅ„ski wrote:

> > 
> 
> a.each\_slice(2){|k,v|h[k]=v}  
> puts h

If you are using Enumerator anyway you can as well do

irb(main):001:0\> a = [“foo”, “bar”, “foo1”, “bar1”,“foo2”, “bar2”]  
=\> [“foo”, “bar”, “foo1”, “bar1”, “foo2”, “bar2”]  
irb(main):002:0\> a.to\_enum(:each\_slice, 2).inject({}) {|h,(k,v)| h[k]=v;  
h}  
=\> {“foo1”=\>“bar1”, “foo2”=\>“bar2”, “foo”=\>“bar”}  
irb(main):003:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [August 1, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/ruby-array-to-hash-a-better-way/110988/7 "2007-08-01T21:15:15Z")

</div>

Hi –

On Thu, 2 Aug 2007, Kyle S. wrote:

> I’ve been using Ruby for awhile, and I’m afraid I’ve never used the \*  
> notation…
> 
> I can see that Hash[a] wouldn’t work, and that Hash[\*a] expands the  
> array out so that Hash can use it elegantly.
> 
> But what, in a general, does the \* do?
> 
> Or is it just for special cases like this?

It “un-arrays” its operand; in other words, given an array, the \*  
gives you a list.

David
