-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new "Identity Comparison" rule #835
Conversation
README.adoc
Outdated
@@ -2198,6 +2198,19 @@ something = something && something.downcase | |||
something &&= something.downcase | |||
---- | |||
|
|||
=== Equal Object Identifier [[equal-object-id]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can name this "Identity Comparison", as the other form of comparison is "Value Comparison" (the one normally done with ==
).
README.adoc
Outdated
@@ -2198,6 +2198,19 @@ something = something && something.downcase | |||
something &&= something.downcase | |||
---- | |||
|
|||
=== Equal Object Identifier [[equal-object-id]] | |||
|
|||
Prefer `equal?` over `==` when comparing `object_id`. `Object#equal?` method is provided to compare objects for identity, and comparing objects for equality using `Object#==` does not require redundant `object_id` to be repeated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comparing objects for equality using
Object#==
does not require redundantobject_id
to be repeated.
I think this part can be worded better, or changed to something like "and in contrast Object#==
is provided for the purpose of doing value comparison" or something along those lines.
This PR adds new "Equal Object Identifier" rule that prefers `equal?` over `==` when comparing `object_id`. `Object#equal?` method is provided to compare objects for identity, and comparing objects for equality using `Object#==` does not require redundant `object_id` to be repeated. ```ruby # bad foo.object_id == bar.object_id # good foo.equal?(bar) ```
f0ee4b3
to
3ac01f4
Compare
Thank you for your review and sophisticated suggestions! I updated this PR. |
You're welcome! |
Created #836 as a followup... |
Follow rubocop/ruby-style-guide#835. This PR adds new `Lint/IdentityComparison` cop. ```ruby # bad foo.object_id == bar.object_id # good foo.equal?(bar) ``` The cop prefers `equal?` over `==` when comparing `object_id`. `Object#equal?` is provided to compare objects for identity, and in contrast `Object#==` is provided for the purpose of doing value comparison.
Follow rubocop/ruby-style-guide#835. This PR adds new `Lint/IdentityComparison` cop. ```ruby # bad foo.object_id == bar.object_id # good foo.equal?(bar) ``` The cop prefers `equal?` over `==` when comparing `object_id`. `Object#equal?` is provided to compare objects for identity, and in contrast `Object#==` is provided for the purpose of doing value comparison.
This PR adds new "Equal Object Identifier" rule that prefers
equal?
over==
when comparingobject_id
.Object#equal?
method is provided to compare objects for identity, and comparing objects for equality usingObject#==
does not requireredundant
object_id
to be repeated.