class RuboCop::Cop::Lint::IdentityComparison
Prefer `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.
@example
# bad foo.object_id == bar.object_id # good foo.equal?(bar)
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/lint/identity_comparison.rb, line 25 def on_send(node) return unless compare_between_object_id_by_double_equal?(node) add_offense(node) do |corrector| receiver = node.receiver.receiver argument = node.first_argument.receiver return unless receiver && argument replacement = "#{receiver.source}.equal?(#{argument.source})" corrector.replace(node, replacement) end end
Private Instance Methods
compare_between_object_id_by_double_equal?(node)
click to toggle source
# File lib/rubocop/cop/lint/identity_comparison.rb, line 41 def compare_between_object_id_by_double_equal?(node) object_id_method?(node.receiver) && object_id_method?(node.first_argument) end
object_id_method?(node)
click to toggle source
# File lib/rubocop/cop/lint/identity_comparison.rb, line 45 def object_id_method?(node) node.send_type? && node.method?(:object_id) end