[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array to Hash in 1.8.6 vs 1.8.7

Fernando Perez

4/28/2009 12:22:00 PM

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.
--
Posted via http://www.ruby-....

3 Answers

Jan-Erik R.

4/28/2009 12:25:00 PM

0

Fernando Perez schrieb:
> 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.
use * like this
Hash[*(session.split("_").map { |couples| couples.split(":") }).flatten]

Fernando Perez

4/28/2009 12:28:00 PM

0

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

Damn splat operator! Thank you so much!!! It works!
--
Posted via http://www.ruby-....

7stud --

4/28/2009 1:44:00 PM

0

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"}

--
Posted via http://www.ruby-....