Does the uniq method remove duplicates because these objects point to the same space in memory or that they contain identical information?
The method is based on the method eql?, so it removes all elements, where is a.eql? (b) returns true. The exact behavior depends on the specific object you are dealing with.
Lines, for example, are considered equal if they contain the same text, regardless of whether they have the same memory allocation.
a = b = "foo"
c = "foo"
[a, b, c].uniq
This is true for most core objects, but not for ruby objects.
class Foo
end
a = Foo.new
b = Foo.new
a.eql? b
Ruby recommends that you redefine the operator ==depending on your class context.
In your particular case, I would suggest creating an object representing the twitter result and implement the comparison logic so that Array.uniq will behave as you expect.
class Result
attr_accessor :text, :notes
def initialize(text = nil, notes = nil)
self.text = text
self.notes = notes
end
def ==(other)
other.class == self.class &&
other.text == self.text
end
alias :eql? :==
end
a = Result.new("first")
b = Result.new("first")
c = Result.new("third")
[a, b, c].uniq