# Convert an array to a hash

**URL:** <https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848>\
**Category:** Ruby\
**Created:** [December 5, 2009, 5:44am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848 "2009-12-05T05:44:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 5:44am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/1 "2009-12-05T05:44:04Z")

</div>

Helo,

How to convert an array to a hash?  
for example,

> x  
> =\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]

to:

> z  
> =\> {“a”=\>“hello”, “b”=\>“world”, “c”=\>“welcome”, “d”=\>“baby”}

And, for creating an array quickly, I could say:

x = %w/a hello b world c welcome d baby/

but how to creat a hash like this way?

Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 6:10am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/2 "2009-12-05T06:10:20Z")

</div>

Ruby N. wrote:

> Helo,
> 
> How to convert an array to a hash?  
> for example,
> 
> > x  
> > =\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]
> 
> to:
> 
> > z  
> > =\> {“a”=\>“hello”, “b”=\>“world”, “c”=\>“welcome”, “d”=\>“baby”}
> 
> And, for creating an array quickly, I could say:
> 
> x = %w/a hello b world c welcome d baby/
> 
> but how to creat a hash like this way?

I’m not sure why you want to do this in the first place, but check out  
the each\_slice method.

> Thanks.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 6:21am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/3 "2009-12-05T06:21:24Z")

</div>

irb(main):001:0\> a=[“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”,  
“baby”]  
=\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]  
irb(main):002:0\> Hash[\*a]  
=\> {“a”=\>“hello”, “b”=\>“world”, “c”=\>“welcome”, “d”=\>“baby”}  
irb(main):003:0\>

* * *

[http://zed.0xff.me](http://zed.0xff.me)

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 9:24am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/4 "2009-12-05T09:24:05Z")

</div>

2009/12/5 Andrey Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):001:0\> a=[“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”,  
> “baby”]  
> =\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]  
> irb(main):002:0\> Hash[\*a]

Thanks.  
What’s the “\*” before “a” then?

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 10:49am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/5 "2009-12-05T10:49:32Z")

</div>

On Sat, Dec 5, 2009 at 9:22 AM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 2009/12/5 Andrey Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > irb(main):001:0\> a=[“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”,  
> > “baby”]  
> > =\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]  
> > irb(main):002:0\> Hash[\*a]
> 
> Thanks.  
> What’s the “\*” before “a” then?

It’s the splat operator. It’s used to “unarray” an array into its  
individual items to pass them to a method:

irb(main):007:0\> def m(a,b,c)  
irb(main):008:1\> p a  
irb(main):009:1\> p b  
irb(main):010:1\> p c  
irb(main):011:1\> end  
=\> nil  
irb(main):012:0\> x = [1,2,3]  
=\> [1, 2, 3]  
irb(main):013:0\> m \*x  
1  
2  
3

or to collect individual elements into an array when it appears in a  
lvalue:

irb(main):014:0\> a,\*b = [1,2,3,4,5]  
=\> [1, 2, 3, 4, 5]  
irb(main):015:0\> a  
=\> 1  
irb(main):016:0\> b  
=\> [2, 3, 4, 5]

or argument list of a method:

irb(main):020:0\> def m(a,\*b)  
irb(main):021:1\> p a  
irb(main):022:1\> p b  
irb(main):023:1\> end  
=\> nil  
irb(main):024:0\> m(1,2,3,4,5,6)  
1  
[2, 3, 4, 5, 6]

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/6 "2009-12-05T16:52:23Z")

</div>

2009/12/5 JesÃºs Gabriel y GalÃ¡n [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It’s the splat operator. It’s used to “unarray” an array into its  
> individual items to pass them to a method:

Ah, I got it, thanks~

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 5, 2009, 6:44am UTC](https://www.ruby-forum.com/t/convert-an-array-to-a-hash/179848/7 "2009-12-05T06:44:12Z")

</div>

Andrey Z. wrote:

> irb(main):001:0\> a=[“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”,  
> “baby”]  
> =\> [“a”, “hello”, “b”, “world”, “c”, “welcome”, “d”, “baby”]  
> irb(main):002:0\> Hash[\*a]  
> =\> {“a”=\>“hello”, “b”=\>“world”, “c”=\>“welcome”, “d”=\>“baby”}  
> irb(main):003:0\>
> 
> * * *
> 
> [http://zed.0xff.me](http://zed.0xff.me)

Oh, right. I thought Hash had a constructor like that, but I couldn’t  
find it in the docs. Thanks!

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
