Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Provide equality comparison methods for objects based on their attributes.
By including this module, a class indicates that its instances have explicit general contracts for ==
, eql?
and hash
methods. Specifically the eql?
contract requires that it implements an equivalence relation. By default, each instance of a class is equal only to itself. This is the right behaviour when you have distinct objects. However, it is the responsibility of any class to clearly define its equality. Failure to do so may prevent instances from behaving as expected when tested for uniqueness in Array#uniq or when used as Hash keys.
Add this line to your application's Gemfile:
gem "equatable"
And then execute:
$ bundle
Or install it yourself as:
$ gem install equatable
It is assumed that your objects are value objects and the only values that affect equality comparison are the ones specified by your attribute readers. Each attribute reader should be a significant field in determining objects values.
class Point
include Equatable
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
end
point_1 = Point.new(1, 1)
point_2 = Point.new(1, 1)
point_3 = Point.new(1, 2)
point_1 == point_2 # => true
point_1.hash == point_2.hash # => true
point_1.eql?(point_2) # => true
point_1.equal?(point_2) # => false
point_1 == point_3 # => false
point_1.hash == point_3.hash # => false
point_1.eql?(point_3) # => false
point_1.equal?(point_3) # => false
point_1.inspect # => "#<Point x=1 y=1>"
It is important that the attribute readers should allow for performing deterministic computations on class instances. Therefore you should avoid specifying attributes that depend on unreliable resources like IP address that require network access.
Equatable ensures that any important property of a type holds for its subtypes. However, please note that adding an extra attribute reader to a subclass will violate the equivalence contract, namely, the superclass will be equal to the subclass but reverse won't be true. For example:
class ColorPoint < Point
attr_reader :color
def initialize(x, y, color)
super(x, y)
@color = color
end
end
point = Point.new(1, 1)
color_point = ColorPoint.new(1, 1, :red)
point == color_point # => true
color_point == point # => false
point.hash == color_point.hash # => false
point.eql?(color_point) # => false
point.equal?(color_point) # => false
The ColorPoint
class demonstrates that extending a class with extra value property does not preserve the equals
contract.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Everyone interacting in the Equatable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Copyright (c) 2012 Piotr Murach. See LICENSE for further details.
FAQs
Unknown package
We found that equatable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.