154

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?

2
  • 2
    The answers below are correct (hash.values being the better IMO). But I wanted to point out when you provide a block to Hash#each it will just return the full value of the hash. If you want to do an operation on each item and return that as an array, use Hash#collect or its alias Hash#map. More stuff on Enumerables here. Commented Mar 5, 2012 at 0:54
  • 4
    This question looks strangely familiar... I wonder how many people are working on the same homework assignment right now. Commented Mar 5, 2012 at 2:41

6 Answers 6

301

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

Sign up to request clarification or add additional context in comments.

4 Comments

+! Nifty! I will upvote despite having a competing answer (using map), cos I like this a lot!
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}'
+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
Are the keys printed in the exact order which they occur ?
52

I would use:

hash.map { |key, value| value }

7 Comments

I didn't downvote it, but it's a more complicated equivalent to Hash#values.
Yes, I also upvoted Ray's answer. I'm happy to upvote a competing answer when I like it too.
I think we are not competing here but helping each other, right? :)
Exactly what I was looking for when trying to create an array from a hash using it's keys as values. Thanks :)
Useful when you need to also do some logic with the keys at the same time.
|
24
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.

2 Comments

Don't forget that, like @Ray Toal's answer, hash.values is ultimately the correct answer here.
Or hash.map(&:second) :)
12

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"]]

Comments

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

1 Comment

stackoverflow.com/posts/12771873/revisions as you can see the formatting of your code was way off
3

There is also this one:

hash = { foo: "bar", baz: "qux" }
hash.map(&:last) #=> ["bar", "qux"]

Why it works:

The & calls to_proc on the object, and passes it as a block to the method.

something {|i| i.foo }
something(&:foo)

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.