views:

54

answers:

1

I have a hash

hash = { 1=> { 0=> 'apple', 1=> 'tree'... ....}, 2=> {.....}}

I want to grab the 0 for all hashes within the hash. I know there is a transpose for array, but there any way to do this with a hash easily?

+2  A: 

Something like this should work:

hash.values.collect{|v| v[0]}

Example:

irb(main):001:0> hash = { 1 => { 0 => 'apple', 1 => 'tree' }, 
  2 => { 0 => 'foo', 1 => 'bar' }}
=> {1=>{0=>"apple", 1=>"tree"}, 2=>{0=>"foo", 1=>"bar"}}
irb(main):002:0> hash.values.collect{|value| value[0]}
=> ["apple", "foo"]
jerhinesmith
undefined method `values' for #<Array:0x450d120>....I have hash within hash though..."test"=>{"1"=>{"0"=>"testing" ....
I see it works in my console as well...but the thing is I'm pulling a params off a table, converting it to an array then sorting into a hash