I have an array of hashes myhash
:
myhash
=> [{"product_id"=>"1", "test"=>"2", "retest"=>"42"}, {"product_id"=>"1", "test"=>"2", "retest"=>"42"}]
I want to map the hashes to their value for product_id
. I did this:
myhash.map{|item| item['product_id']}
# => ["1", "1"]
which gives what I want.
Is there anyway to make it nicer using a map proc? I tried myhash.map(&:fetch('product_id'))
, but to no avail.
Edit: In other words, I resumed the situation thanks to @7stud who tried to answer:
a.map(&:"fetch('product_id')")
=> NoMethodError: undefined method `fetch('product_id')' for {"product_id"=>"1", "test"=>"2", "retest"=>"42"}:Hash
from (irb):5:in `map'
from (irb):5
from /home/shideneyu/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>'
Or, when I do this:
{"product_id"=>"1", "test"=>"2", "retest"=>"42"}.fetch('product_id')
=> "1"
It retrieves the good value. The issue then is that I can't pass a param to the fetch
method while using the map. How to do so?