#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Looping hash into a string


    Hello, new to ruby so please bare with my newbness. I wanted to take each key/value and put it into a string for my link builder script. Here's what I have so far:

    Code:
    values = ['one' => '1', 'two' => '2']
      @link = values.collect do |k, v|
      "#{k}=#{v}&"
      end
    printing @link for some reason produces:
    one1two2=&

    I wanted to have it result like this:
    one=1&two=2


    Please advise.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Theres a subtlety there that I think you are missing:
    Code:
     
    irb(main):001:0> {'one' => 1, 'two' => 2}
    => {"two"=>2, "one"=>1}
    
    irb(main):002:0>  ['one' => 1, 'two' => 2]
    => [{"two"=>2, "one"=>1}]
    
    irb(main):003:0> {'one' => 1, 'two' => 2}.class
    => Hash
    
    irb(main):004:0>  ['one' => 1, 'two' => 2].class
    => Array
    Similarly
    Code:
    irb(main):008:0> {'one' => 1, 'two' => 2}.collect { |k,v| puts "#{k}:#{v}" }
    two:2
    one:1
    => [nil, nil]
    irb(main):009:0> ['one' => 1, 'two' => 2].collect { |k,v| puts "#{k}:#{v}" }
    two2one1:
    => [nil]
    Realize that the array has a single element (the hash) that, as a string, is just everything concatenated. The hash, on the other hand, iterates by handing you the key/value pairs you are expecting.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by L7Sqr
    Theres a subtlety there that I think you are missing:
    Code:
     
    irb(main):001:0> {'one' => 1, 'two' => 2}
    => {"two"=>2, "one"=>1}
    
    irb(main):002:0>  ['one' => 1, 'two' => 2]
    => [{"two"=>2, "one"=>1}]
    
    irb(main):003:0> {'one' => 1, 'two' => 2}.class
    => Hash
    
    irb(main):004:0>  ['one' => 1, 'two' => 2].class
    => Array
    Similarly
    Code:
    irb(main):008:0> {'one' => 1, 'two' => 2}.collect { |k,v| puts "#{k}:#{v}" }
    two:2
    one:1
    => [nil, nil]
    irb(main):009:0> ['one' => 1, 'two' => 2].collect { |k,v| puts "#{k}:#{v}" }
    two2one1:
    => [nil]
    Realize that the array has a single element (the hash) that, as a string, is just everything concatenated. The hash, on the other hand, iterates by handing you the key/value pairs you are expecting.
    That worked! Thanks!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0
    Code:
    irb(main):014:0> values.each_pair{|x,y| print "#{x}:#{y}\n"}
    one:1
    two:2
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Hey, kurum1, if you are going to chime in might as well make it worth our time.
    If you look at the initial post you will see the following construct:
    Code:
     values = [ "one" => 1, "two" => 2 ]
    If that was used in your example you would see
    Originally Posted by fail
    NoMethodError: undefined method `each_pair' for [{"two"=>2, "one"=>1}]:Array
    I imagine that a example on how to raise an exception was not what the OP was after...
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo