When I have an array of hashes or AR objects, I like to do this to get access to certain fields:
products.map(&:upc_code)
Sometimes I have an array of hashes that are instead keyed by a string, and then I can't use the & sign. So instead I have to do:
people.map{ |p| p['Last Name'] }
I know the ampersand can be used to pass a block, but my question is why does it work the way it does in the first statement above. What is it actually doing?
products.map(&:upc_code)is equivalent toproducts.map{ |e| e.upc_code }. A hash doesn't respond to the methods named after keys, thus it doesn't work that way with a hash.