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"]]
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"]]
Author by
tbrooke
Updated on June 03, 2020Comments
-
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 over 12 yearsDon't forget that, like @Ray Toal's answer,
hash.values
is ultimately the correct answer here. -
Michael Durrant over 12 years+! Nifty! I will upvote despite having a competing answer (using
map
), cos I like this a lot! -
Mark Thomas over 12 yearsI didn't downvote it, but it's a more complicated equivalent to
Hash#values
. -
Michael Durrant over 12 yearsYes, I also upvoted Ray's answer. I'm happy to upvote a competing answer when I like it too.
-
jordanbtucker almost 12 years
Hash#values
is not only simpler, but more efficient. Comparetime ruby -e '(1..1000000).reduce({}){|h,i| h.store i,i; h}.values'
withtime ruby -e '(1..1000000).reduce({}){|h,i| h.store i,i; h}.map{|k,v| v}'
-
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 over 9 yearsOr
hash.map(&:second)
:) -
stack1 about 9 yearsAre the keys printed in the exact order which they occur ?
-
karlingen over 8 yearsstackoverflow.com/posts/12771873/revisions as you can see the formatting of your code was way off
-
witkacy26 over 8 yearsI think we are not competing here but helping each other, right? :)
-
Fabian over 8 yearsExactly what I was looking for when trying to create an array from a hash using it's keys as values. Thanks :)
-
Chris Cirefice almost 8 yearsUseful when you need to also do some logic with the keys at the same time.
-
Sylvain over 6 yearsA more in depth explanation brianstorti.com/understanding-ruby-idiom-map-with-symbol
-
Ethan Chen almost 6 yearsSince you're not using the
key
variable, you can even dohash.map { |_, value| value }
-
tobixen about 3 yearsUpvoted. Although hash.values is a more appropriate answer to the actual question asked, this answer helped me in the problem I was googling for.