3

I have an array of hashes from which I need the values of the hashes in a new array. The array of hashes look likes this, with a couple thousand of them.

array = [{:code=>"404"}, {:code=>"302"}, {:code=>"200"}]

I have tried to look this up but have only found out how to convert from a hash.

How would I go about doing this?

2
  • Can you add what the output should be? Commented Oct 16, 2016 at 4:11
  • array.flat_map { |h| h.values } Commented Oct 16, 2016 at 5:42

4 Answers 4

8
[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}].flat_map(&:values)
#⇒ ["404", "302", "200"]
Sign up to request clarification or add additional context in comments.

Comments

4
a=[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}] 
puts a.map{|x|x.values}.flatten.inspect

output

["404", "302", "200"]

8 Comments

When I try to use this I get a undefined method for the values part. Any suggestions?
Did you use the same code? Because I executed my code and pasted it here, it works fine. Which version of Ruby are you using?
I tried it in irb and it did work for me too. It must be something in my code causing it not to work. I'm using 2.3.1.
I got it to work. their was two brackets at either end because i was pushing the array in to another array.
whenever you use map followed by flatten, consider using flat_map instead.
|
3
arr =[{:code=>"404"}, {:code=>"302"}, {:code=>"200"}]

arr.map { |h| h[:code] }
  #=> ["404", "302", "200"]

or, if the name of the key (now :code) might change in future:

arr.map { |h| h.first.last }
  #=> ["404", "302", "200"]

10 Comments

Good one! I haven't noticed the fact that keys are equal. And also I don't know that h.first converts hash key,value as array.
@Gopal, that's always the case when a hash's elements (key-value pairs) are enumerated. For example, {a: 1, b: 2}.each { |a| p a } prints [:a, 1] then [:b, 2].
Yes, but what's the relation between enumeration and .first? why does .first returns array of key,value?
@Gopal, first is a method in the Enumerable module. That module is included by the class Hash. All methods in Enumerable require that the receiver be an enumerator. When first is called on a hash, Ruby (after seeing that there is no method Hash#first and knowing that Enumerable#first requires an enumerator) inserts the instance method Hash#each: {a: 1}.first is evaluated as {a: 1}.each.first. That's why all classes that include Enumerable must have a method each that returns an enumerator.
You say all the methods in Enumerable require the receiver be an enumerator, but each method is from enumerator class but it can be called upon array like a.each, but a is not an enumerator object, So it contradict with what you said, right?
|
0

This question is tagged with ruby only.

But I think it might be worth noting that, when you happen to use Ruby on Rails or have the activesupport gem in your Gemfile anyway, then you might want to use Enumerable#pluck:

array = [{:code=>"404"}, {:code=>"302"}, {:code=>"200"}]
array.pluck(:code)
#=> ["404", "302", "200"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.