views:

53

answers:

3

This is the output from doing puts get_account_entry.inspect

[[{:value=>"8b08e26a-6d35-7140-9e41-4c5b4612c146", :name=>"id"}, {:value=>"Typhoon Corporation", :name=>"name"}]]

How do I extract out the value of :name => "id" for example? Originally I thought it was like a hash, so get_account_entry[id] would produce the result, but it doesn't which makes sense on closer inspection.

But then how do I get at the values?

Chuck got me on the right path, but still need help:

  puts get_account_entry[0].map {|hash| [hash[:name], hash[:value]] }

This is the output in the ruby console:

> id
> 8b08e26a-6d35-7140-9e41-4c5b4612c146
> name 
> Typhoon Corporation
A: 

The most external square brackets "[]" says that this serialization represents an array.

Have you tried something like get_entries[0][id] (or get_entries[0][0][id] since it's a double bracket)?

Sir Gallahad
A: 

This is how I would approach.

a= [[{:value=>"8b08e26a-6d35-7140-9e41-4c5b4612c146", :name=>"id"}, {:value=>"Typhoon Corporation", :name=>"name"}]]

a[0].each do |hash|
  hash.each_pair do |k,v|
    puts v
  end
end

Hope this helps.

alokswain
Ah! should soon learn to think in terms of code-blocks rather than painful iterations. Chuck's answer is the way to go.
alokswain
great thanks -- I think so as well, appreciate your response and the +1 on chuck's!
Angela
+2  A: 

You're close to right. It's an array containing an array of Hash, which together form a sort of Hash-like structure. To get the value with the corresponding name "id", you'd have to do get_entries[0].find {|field| field[:name] == 'id'}[:value]. The initial [0] gets us inside the pointless outer array, and then we need to find which hash has the :name entry "id", then we ask it for the value of its :value entry.

If you want to convert this name-value data structure into a normal hash, you could do Hash[get_entries[0].map {|hash| [hash[:name], hash[:value]] }].

Chuck
ah, interesting -- I like the idea of converting into a normal hash...let me try that...
Angela
Hi, sorry, I tried and got the following error:odd number of arguments for HashDo I access it using Hash[id][:value]?
Angela
I used a puts before the Hash line above to see what came out and it says 'odd number of arguments'
Angela