0
我有對象的數組,例如:獲取匹配的兩個元素的對象數組的獨特元素只有
[#<Something id: 34175, name: "abc", value: 123.3, comment: "something here">,
#<Something id: 34176, name: "xyz", value: 123.3, comment: "something here">,
#<Something id: 34177, name: "xyz", value: 227.3, comment: "something here sdfg">,
#<Something id: 34178, name: "xyz", value: 123.3, comment: "something here sdfg">]
我想返回不具有相同的名稱和值的所有元素。所以在這種情況下,退貨將是:
[#<Something id: 34175, name: "abc", value: 123.3, comment: "something here">,
#<Something id: 34176, name: "xyz", value: 123.3, comment: "something here">,
#<Something id: 34177, name: "xyz", value: 227.3, comment: "something here sdfg">]
我所關心的是名稱和價值。
我試着將一個塊傳遞給uniq
方法,但我不知道如何通過兩個元素而不是一個元素進行匹配。
這應該這樣做:'a.uniq {|實例| [instance.name,instance.value]}'。 – 2014-09-29 16:44:35
@CarySwoveland作爲回答 – 2014-09-29 16:45:14
@CarySwoveland做到了!謝謝你的幫助。作爲回答發佈,我會接受。 – lundie 2014-09-29 16:50:48