1

I'm learning Ruby and I would like to know how to pick each :values I have stored in all the hashes inside an array at once. The data structure looks like this (with more hashes):

[ { :label => "Grid Singularity ", :values => [44, 1] }, { :label => "iamsmart ", :values => [44, 1] } ] 

If I do:

data_hash[0][:values] #=> [44, 1] 

I can get the values of the first hash, but, how can I select each :values array at once? My goal is to be able to do the same operation with each :values without having to select them one by one (it's a very long list!)

Thank you so much for your help!

1
  • 1
    Your data_hash is actually an array. Commented Sep 23, 2017 at 20:46

1 Answer 1

2

You can use map to get a new array just taking the value of each values keys from the main hash, like:

hash = [ {:label=>"Grid Singularity ", :values=>[44, 1]}, {:label=>"iamsmart ", :values=>[44, 1]} ] p hash.map { |el| el[:values] } # => [[44, 1], [44, 1]] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.