Forum: Ruby Array to Hash in 1.8.6 vs 1.8.7

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-04-28 14:21
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.
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-04-28 14:25
(Received via mailing list)
Fernando Perez schrieb:
>
>
> Many thanks in advance.
use * like this
  Hash[*(session.split("_").map { |couples| couples.split(":")
}).flatten]
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-04-28 14:27
> use * like this
>   Hash[*(session.split("_").map { |couples| couples.split(":")
> }).flatten]

Damn splat operator! Thank you so much!!! It works!
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-28 15:43
Fernando Perez 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"}
This topic is locked and can not be replied to.