Skip to content
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

Merged
merged 1 commit into from
Aug 8, 2020

Conversation

koic
Copy link
Member

@koic koic commented Aug 8, 2020

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.

# bad
foo.object_id == bar.object_id

# good
foo.equal?(bar)

README.adoc Outdated
@@ -2198,6 +2198,19 @@ something = something && something.downcase
something &&= something.downcase
----

=== Equal Object Identifier [[equal-object-id]]
Copy link
Collaborator

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.
Copy link
Collaborator

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 redundant object_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)
```
@koic koic changed the title Add new "Equal Object Identifier" rule Add new "Identity Comparison" rule Aug 8, 2020
@koic koic force-pushed the add_new_equal_object_id_rule branch from f0ee4b3 to 3ac01f4 Compare August 8, 2020 08:09
@koic
Copy link
Member Author

koic commented Aug 8, 2020

Thank you for your review and sophisticated suggestions! I updated this PR.

@bbatsov bbatsov merged commit fb6ca99 into rubocop:master Aug 8, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 8, 2020

You're welcome!

@koic koic deleted the add_new_equal_object_id_rule branch August 8, 2020 08:19
marcandre added a commit that referenced this pull request Aug 8, 2020
@marcandre
Copy link
Contributor

Created #836 as a followup...

bbatsov pushed a commit that referenced this pull request Aug 8, 2020
koic added a commit to koic/rubocop that referenced this pull request Sep 11, 2020
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.
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request Sep 11, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants