# Array to Hash in 1.8.6 vs 1.8.7

**URL:** <https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675>\
**Category:** Ruby\
**Created:** [April 28, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675 "2009-04-28T14:21:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 28, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675/1 "2009-04-28T14:21:59Z")

</div>

I’m damn toast!

My dev machine runs a manually compiled 1.8.7 version of Ruby. My server  
runs a Debian Etch with Ruby 1.8.6 version.

From a string:

1:2\_5:6

I want to convert it to a hash: {“1” =\> “2”, “5” =\> “6”}

On my dev machine the following works:  
Hash[(session.split("\_").map { |couples| couples.split(":") }).flatten]

On my server it pukes on me with the following error:  
odd number of arguments for Hash

How can I fix it with Ruby 1.8.6 and 1.8.7 compatibility? I don’t want  
to play with the Ruby version of Debian etch as it’s the latest  
available package.

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 28, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675/2 "2009-04-28T14:25:45Z")

</div>

Fernando P. schrieb:

> Many thanks in advance.  
> use \* like this  
> Hash[\*(session.split("\_").map { |couples| couples.split(":")  
> }).flatten]

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 28, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675/3 "2009-04-28T14:27:51Z")

</div>

> use \* like this  
> Hash[\*(session.split("\_").map { |couples| couples.split(":")  
> }).flatten]

Damn splat operator! Thank you so much!!! It works!

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 28, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/array-to-hash-in-1-8-6-vs-1-8-7/166675/4 "2009-04-28T15:43:58Z")

</div>

Fernando P. wrote:

> I’m damn toast!
> 
> My dev machine runs a manually compiled 1.8.7 version of Ruby. My server  
> runs a Debian Etch with Ruby 1.8.6 version.
> 
> From a string:
> 
> 1:2\_5:6
> 
> I want to convert it to a hash: {“1” =\> “2”, “5” =\> “6”}
> 
> On my dev machine the following works:  
> Hash[(session.split("\_").map { |couples| couples.split(":") }).flatten]
> 
> On my server it pukes on me with the following error:  
> odd number of arguments for Hash
> 
> How can I fix it with Ruby 1.8.6 and 1.8.7 compatibility? I don’t want  
> to play with the Ruby version of Debian etch as it’s the latest  
> available package.
> 
> Many thanks in advance.

Here’s something less tortured (although still too gruesome for my  
tastes):

str = ‘1:2\_5:6’  
result = Hash[\*str.split(/[:\_]/)]

p result

–output:–  
{“1”=\>“2”, “5”=\>“6”}
