How do I get the unique elements from an array of hashes in Ruby?

后端 未结 8 1106
抹茶落季
抹茶落季 2020-12-28 14:57

I have an array of hashes, and I want the unique values out of it. Calling Array.uniq doesn\'t give me what I expect.

a = [{:a => 1},{:a =&g         


        
8条回答
  •  没有蜡笔的小新
    2020-12-28 15:21

    The pipe method on arrays (available since 1.8.6) performs set union (returning an array), so the following is another possible way to get unique elements of any array a:

    [] | a

提交回复
热议问题