Ruby Hash to array of values

194,999

Solution 1

Also, a bit simpler....

>> hash = { "a"=>["a", "b", "c"], "b"=>["b", "c"] }
=> {"a"=>["a", "b", "c"], "b"=>["b", "c"]}
>> hash.values
=> [["a", "b", "c"], ["b", "c"]]

Ruby doc here

Solution 2

I would use:

hash.map { |key, value| value }

Solution 3

hash.collect { |k, v| v }
#returns [["a", "b", "c"], ["b", "c"]] 

Enumerable#collect takes a block, and returns an array of the results of running the block once on every element of the enumerable. So this code just ignores the keys and returns an array of all the values.

The Enumerable module is pretty awesome. Knowing it well can save you lots of time and lots of code.

Solution 4

It is as simple as

hash.values
#=> [["a", "b", "c"], ["b", "c"]]

this will return a new array populated with the values from hash

if you want to store that new array do

array_of_values = hash.values
#=> [["a", "b", "c"], ["b", "c"]]

array_of_values
 #=> [["a", "b", "c"], ["b", "c"]]

Solution 5

hash  = { :a => ["a", "b", "c"], :b => ["b", "c"] }
hash.values #=> [["a","b","c"],["b","c"]]
Share:
194,999
tbrooke
Author by

tbrooke

Updated on June 03, 2020

Comments

  • tbrooke
    tbrooke about 4 years

    I have this:

    hash  = { "a"=>["a", "b", "c"], "b"=>["b", "c"] } 
    

    and I want to get to this: [["a","b","c"],["b","c"]]

    This seems like it should work but it doesn't:

    hash.each{|key,value| value}
    => {"a"=>["a", "b", "c"], "b"=>["b", "c"]} 
    

    Any suggestions?

  • tadman
    tadman over 12 years
    Don't forget that, like @Ray Toal's answer, hash.values is ultimately the correct answer here.
  • Michael Durrant
    Michael Durrant over 12 years
    +! Nifty! I will upvote despite having a competing answer (using map), cos I like this a lot!
  • Mark Thomas
    Mark Thomas over 12 years
    I didn't downvote it, but it's a more complicated equivalent to Hash#values.
  • Michael Durrant
    Michael Durrant over 12 years
    Yes, I also upvoted Ray's answer. I'm happy to upvote a competing answer when I like it too.
  • jordanbtucker
    jordanbtucker almost 12 years
    Hash#values is not only simpler, but more efficient. Compare time ruby -e '(1..1000000).reduce({}){|h,i| h.store i,i; h}.values' with time ruby -e '(1..1000000).reduce({}){|h,i| h.store i,i; h}.map{|k,v| v}'
  • Romans 8.38-39
    Romans 8.38-39 over 10 years
    +1 (Dazzled amazingly after I tried your code) I spent one day with no luck removing the index using many methods... All I can say thank you very much!!! :D
  • Jaap Haagmans
    Jaap Haagmans over 9 years
    Or hash.map(&:second) :)
  • stack1
    stack1 about 9 years
    Are the keys printed in the exact order which they occur ?
  • karlingen
    karlingen over 8 years
    stackoverflow.com/posts/12771873/revisions as you can see the formatting of your code was way off
  • witkacy26
    witkacy26 over 8 years
    I think we are not competing here but helping each other, right? :)
  • Fabian
    Fabian over 8 years
    Exactly what I was looking for when trying to create an array from a hash using it's keys as values. Thanks :)
  • Chris Cirefice
    Chris Cirefice almost 8 years
    Useful when you need to also do some logic with the keys at the same time.
  • Sylvain
    Sylvain over 6 years
  • Ethan Chen
    Ethan Chen almost 6 years
    Since you're not using the key variable, you can even do hash.map { |_, value| value }
  • tobixen
    tobixen about 3 years
    Upvoted. Although hash.values is a more appropriate answer to the actual question asked, this answer helped me in the problem I was googling for.