Ruby extracts hash values ​​and saves them to a new Hash

advertisements

This ia a real ruby newbie question. I have a Hash that looks like this

@hash1 = {1=>{"a"=>1,"b"=>2,"c"=>3}}

I want to extract the value and store it in a new Hash like this

{"a"=>1,"b"=>2,"c"=>3}

I tried doing

@hash2 = @hash1.values

But that gave me an Array that looked like

[{"a"=>1,"b"=>2,"c"=>3}]

I couldn't figure out how to convert that to a Hash. I'm probably missing something really simple. Any ideas how to do this?


Simple. Just do what you did, but with [0] stuck on the end (get first element):

@hash.values[0] #=> {"a"=>1,"b"=>2,"c"=>3}