I have an array of objects which may contain objects with same attribute values. I am trying to remove the duplicates based on multiple attributes (not just one attribute value)
class Font
attr_accessor :color, :name, :type
end
a = <@color="blue", @name="s", @type="bold">
b = <@color="blue", @name="r", @type="italic">
c = <@color="green", @name="t", @type="bold">
d = <@color="blue", @name="s", @type="some_other_type">
fonts = [a, b, c, d]
I need to eliminate duplicates based on the values of color, name (I don't care about type)
what I have tried
uniq_fonts = fonts.uniq { |f| f.name.to_s + f.color.to_s}
is there any cleaner way in which I can achieve the same result?
Note: these are objects and not hashes. I know we could have used:
fonts.uniq { |f| f.values_at(:name, :color)}
if they were hash
a,b,canddare not valid objects, so readers cannot test without going to the trouble of modifying your code. You need to adddef initialize(color, name, type); @color, @name, @type = color, name, type; end, definea = Font("blue", "s", "bold")and similar forb,candd.a = Font.new("blue", "s", "bold")...