Last active
October 30, 2015 10:35
-
-
Save ttanimichi/90dde8eaacd95e95149c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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