-2

I want to iterate over this data to extract the value of the ids:

[ {:id => 3, :quantity => 5 }, { :id => 4, :quantity => 3 } ]
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
myhouse
  • 1,911
  • 1
  • 17
  • 24
  • This is a low-quality question, but it is *not* an exact duplicate of [this other](https://stackoverflow.com/questions/1227571/how-to-iterate-over-a-hash-in-ruby) low-quality question. It needs a better close target; voting to reopen. – Todd A. Jacobs Sep 18 '16 at 22:31
  • I guess it's not an exact duplicated but are we letting it opened anyway? Actually the accepted answer is the same in the other question... – Jorge de los Santos Sep 18 '16 at 22:33
  • I don't understand why my question sucked, but you took the time to answer, @CodeGnome, and 5 people voted on the answer (I'm assuming it was helpful for five people). It was a basic question and very simple. Do you want me to ask complex questions? The other question posted was similar than mine, but also different and is not what I needed. – myhouse Sep 18 '16 at 23:39
  • @myhouse It's low quality because it doesn't show much research effort, and while it provides a corpus there's no code showing what you've done to solve the problem. The question was at least *clear*, so I don't really have a problem with it. However, be aware that trivial questions, especially those that don't show where you're struggling with code or error messages, tend to be downvoted. YMMV. – Todd A. Jacobs Sep 19 '16 at 02:08
  • I wasn't sure how to grab the hashes inside of the array. I tried doing ar.each do |k, v| and printing out separately like people were doing in the other question but it wouldn't produce the expected results. I wanted someone to quickly show me something really basic and I got what I needed. I think the down comment was kinda a jerk move. – myhouse Sep 19 '16 at 02:13
  • 1
    @JorgedelosSantos Questions shouldn't be marked duplicates because of similar (or even identical) answers. You can find related posts about this on meta. Questions should be marked duplicates when the *question* is substantially the same. – Todd A. Jacobs Sep 19 '16 at 02:13
  • @myhouse See? That would have been helpful in avoiding close votes, since it would show why the other question is *not* a duplicate of yours. The questions have different data sets, and different objectives, even though both involve hashes. – Todd A. Jacobs Sep 19 '16 at 02:15
  • This only has been out for 4 hours and 8 people found it useful. – myhouse Sep 19 '16 at 02:15

3 Answers3

5

You can use Array's map method like below:

arr =[ {:id => 3, :quantity => 5 }, { :id => 4, :quantity => 3 } ]
ids = arr.map { |k| k[:id] }
#=> [3,4]
LearningROR
  • 211
  • 1
  • 13
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
2
[ {:id => 3, :quantity => 5 }, { :id => 4, :quantity => 3 } ].each do |hash|
  puts hash[:id]
end

This will puts each id value on the screen. You can do what you need to do from there.

davidhu
  • 9,523
  • 6
  • 32
  • 53
1

Ruby: Mapping Over an Array with Hash#values_at

There's more than one way to do this in Ruby. A lot depends on what you're trying to express. If you're not trying to play code golf, one way to do this is with Hash#values_at. For example:

[{id: 3, quantity: 5}, {id: 4, quantity: 3}].flat_map { |h| h.values_at :id }
#=> [3, 4]

Rails: Extracting Data with Pluck

A more Rails-like way would be to just ActiveRecord::Calculations#pluck the attributes you want in the query itself. For example:

Stuff.where(quantity: [3, 5]).pluck :id

There are certainly other ways to get the same result. Your mileage may vary, depending on your real use case.

See Also

Active Record Query Interface

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199