Skip to content

Instantly share code, notes, and snippets.

@ttanimichi
Last active October 30, 2015 10:35
Show Gist options
  • Save ttanimichi/90dde8eaacd95e95149c to your computer and use it in GitHub Desktop.
Save ttanimichi/90dde8eaacd95e95149c to your computer and use it in GitHub Desktop.
module ComparingWith
module InstanceVariables
def ==(other)
[self, other].map {|object|
names = object.instance_variables
values = names.map {|name| object.instance_variable_get name }
[names, values].transpose.to_h
}.reduce(&:==)
end
end
end
class Foo
include ComparingWith::InstanceVariables
def initialize(a, b)
@a, @b = a, b
end
end
puts Foo.new(1, 2) == Foo.new(1, 2) #=> true
puts Foo.new(1, 2) == Foo.new(3, 4) #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment