Sha256: 43b804cadc3542876e093865fef5f42207389755e5e4cd7284dbd8a8acace853
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
# frozen_string_literal: true require 'set' require_relative 'ivar_equatable/version' # Include in a class to make its instances capable # of comparing themselves with other objects of the same class # by calling `==` on their instance variables. # # Example: # # class MyClass # include IvarEquatable # # attr_accessor :foo, :bar # # def initialize(foo: nil, bar: nil) # @foo = foo # @bar = bar # end # end # # obj1 = MyClass.new foo: :some_value # obj2 = MyClass.new foo: :some_value # obj1 == obj2 #=> true # # obj1 = MyClass.new foo: :some_value # obj2 = MyClass.new foo: :other_value # obj1 == obj2 #=> false # # obj1 = MyClass.new foo: :some_value # obj2 = MyClass.new foo: :some_value, bar: 2 # obj1 == obj2 #=> false # module IvarEquatable # @param other [Object] # @return [Boolean] def eql?(other) return true if equal?(other) return false unless other.is_a?(self.class) || is_a?(other.class) # @type [Set<Symbol>] self_ivars = instance_variables.to_set # @type [Set<Symbol>] other_ivars = other.instance_variables.to_set return false unless self_ivars == other_ivars self_ivars.each do |ivar| return false if instance_variable_get(ivar) != other.instance_variable_get(ivar) end true end alias == eql? end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ivar_equatable-0.1.0 | lib/ivar_equatable.rb |