开发者

How do you pull a single attribute out of an array of hashes?

开发者 https://www.devze.com 2023-01-11 05:01 出处:网络
I would like to know ice cream preferences of all my cats in an array. So the output would开发者_如何学C be just the :ice_cream out of all the cat\'s :

I would like to know ice cream preferences of all my cats in an array.

So the output would开发者_如何学C be just the :ice_cream out of all the cat's :

[ "vanilla", "chocolate", "mint chocolate", "mice" ]

While the cat objects are :

cat => {:ice_cream => "chocolate", :paws => "4", :wants_to_kill_humans => "yes" }

Yikes this is such an easy answer I bet, but I can't find it anywhere.


You want something like:

@cats.collect {|cat| cat[:ice_cream] }
0

精彩评论

暂无评论...
验证码 换一张
取 消