Array to Hash in 1.8.6 vs 1.8.7

Discussion in 'Ruby' started by Fernando Perez, Apr 28, 2009.

  1. 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/.
     
    Fernando Perez, Apr 28, 2009
    #1
    1. Advertising

  2. Fernando Perez

    badboy Guest

    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]
     
    badboy, Apr 28, 2009
    #2
    1. Advertising

  3. > 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/.
     
    Fernando Perez, Apr 28, 2009
    #3
  4. Fernando Perez

    7stud -- Guest

    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/.
     
    7stud --, Apr 28, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    278
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    237
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
  5. Replies:
    11
    Views:
    191
    Eric Schwartz
    Oct 10, 2005
Loading...

Share This Page