Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created December 13, 2013 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6ewis/7941346 to your computer and use it in GitHub Desktop.
Save 6ewis/7941346 to your computer and use it in GitHub Desktop.
here is what I suggest you could do
require 'active_support/all'
class Article
attr_accessor :id
def initialize(attributes={})
@id = attributes[:id]
end
def ==(comparison_object)
super ||
comparison_object.instance_of?(self.class) &&
self.id.present? &&
self.id == comparison_object.id
end
alias :eql? :==
end
a1 = Article.new(id: 1)
a2 = Article.new(id: 1)
a3 = Article.new(id: 1)
puts [a1, a2, a3].uniq_by {|a,b| a == b} #=> [#<Article ...>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment