Ruby on rails 获取仅匹配两个元素的对象数组的唯一元素

Ruby on rails 获取仅匹配两个元素的对象数组的唯一元素,ruby-on-rails,ruby,arrays,Ruby On Rails,Ruby,Arrays,我有一个对象数组,例如: [#<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: "som

我有一个对象数组,例如:

[#<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
方法,但我不知道如何通过两个元素而不仅仅是一个元素进行匹配。

您想使用以块为单位的形式

代码

arr.uniq { |instance| [instance.name, instance.value] }
class Something
  attr_accessor :id, :name, :value, :comment
  def initialize(id, name, value, comment)
    @id = id
    @name = name
    @value = value
    @comment = comment
  end
end

arr = [Something.new(34175, "abc", 123.3, "something here"),
       Something.new(34176, "xyz", 123.3, "something here"),
       Something.new(34177, "xyz", 227.3, "something here sdfg"),
       Something.new(34178, "xyz", 123.3, "something here sdfg")]
  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">,
  #    #<Something:0x000001012cc0e8 @id=34178, @name="xyz", @value=123.3,
  #      @comment="something here sdfg">]

arr.uniq { |instance| [instance.name, instance.value] }

  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">]
示例

arr.uniq { |instance| [instance.name, instance.value] }
class Something
  attr_accessor :id, :name, :value, :comment
  def initialize(id, name, value, comment)
    @id = id
    @name = name
    @value = value
    @comment = comment
  end
end

arr = [Something.new(34175, "abc", 123.3, "something here"),
       Something.new(34176, "xyz", 123.3, "something here"),
       Something.new(34177, "xyz", 227.3, "something here sdfg"),
       Something.new(34178, "xyz", 123.3, "something here sdfg")]
  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">,
  #    #<Something:0x000001012cc0e8 @id=34178, @name="xyz", @value=123.3,
  #      @comment="something here sdfg">]

arr.uniq { |instance| [instance.name, instance.value] }

  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">]
分类
属性访问器:id,:name,:value,:comment
def初始化(id、名称、值、注释)
@id=id
@name=name
@价值=价值
@注释=注释
结束
结束
arr=[Something.new(34175,“abc”,123.3,“Something here”),
新的东西(34176,“xyz”,123.3,“这里的东西”),
新的东西(34177,“xyz”,227.3,“这里的东西sdfg”),
新的东西(34178,“xyz”,123.3,“这里的东西sdfg”)]
#=> [#,
#    #,
#    #,
#    #]
arr.uniq{| instance |[instance.name,instance.value]}
#=> [#,
#    #,
#    #]

您希望使用以块为单位的形式

代码

arr.uniq { |instance| [instance.name, instance.value] }
class Something
  attr_accessor :id, :name, :value, :comment
  def initialize(id, name, value, comment)
    @id = id
    @name = name
    @value = value
    @comment = comment
  end
end

arr = [Something.new(34175, "abc", 123.3, "something here"),
       Something.new(34176, "xyz", 123.3, "something here"),
       Something.new(34177, "xyz", 227.3, "something here sdfg"),
       Something.new(34178, "xyz", 123.3, "something here sdfg")]
  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">,
  #    #<Something:0x000001012cc0e8 @id=34178, @name="xyz", @value=123.3,
  #      @comment="something here sdfg">]

arr.uniq { |instance| [instance.name, instance.value] }

  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">]
示例

arr.uniq { |instance| [instance.name, instance.value] }
class Something
  attr_accessor :id, :name, :value, :comment
  def initialize(id, name, value, comment)
    @id = id
    @name = name
    @value = value
    @comment = comment
  end
end

arr = [Something.new(34175, "abc", 123.3, "something here"),
       Something.new(34176, "xyz", 123.3, "something here"),
       Something.new(34177, "xyz", 227.3, "something here sdfg"),
       Something.new(34178, "xyz", 123.3, "something here sdfg")]
  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">,
  #    #<Something:0x000001012cc0e8 @id=34178, @name="xyz", @value=123.3,
  #      @comment="something here sdfg">]

arr.uniq { |instance| [instance.name, instance.value] }

  #=> [#<Something:0x000001012cc2f0 @id=34175, @name="abc", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc278 @id=34176, @name="xyz", @value=123.3,
  #      @comment="something here">,
  #    #<Something:0x000001012cc200 @id=34177, @name="xyz", @value=227.3,
  #      @comment="something here sdfg">]
分类
属性访问器:id,:name,:value,:comment
def初始化(id、名称、值、注释)
@id=id
@name=name
@价值=价值
@注释=注释
结束
结束
arr=[Something.new(34175,“abc”,123.3,“Something here”),
新的东西(34176,“xyz”,123.3,“这里的东西”),
新的东西(34177,“xyz”,227.3,“这里的东西sdfg”),
新的东西(34178,“xyz”,123.3,“这里的东西sdfg”)]
#=> [#,
#    #,
#    #,
#    #]
arr.uniq{| instance |[instance.name,instance.value]}
#=> [#,
#    #,
#    #]

这应该可以做到:
a.uniq{| instance |[instance.name,instance.value]}
@CarySwoveland作为answer@CarySwoveland成功了!谢谢你的帮助。发布作为答案,我会接受。这应该可以做到:
a.uniq{| instance |[instance.name,instance.value]}
@CarySwoveland发布作为答案answer@CarySwoveland成功了!谢谢你的帮助。作为答案发布,我将接受。