Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array to Hash in 1.8.6 vs 1.8.7

Reply
Thread Tools

Array to Hash in 1.8.6 vs 1.8.7

 
 
Fernando Perez
Guest
Posts: n/a
 
      04-28-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
badboy
Guest
Posts: n/a
 
      04-28-2009
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]

 
Reply With Quote
 
 
 
 
Fernando Perez
Guest
Posts: n/a
 
      04-28-2009
> 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-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-28-2009
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments