class RuboCop::Cop::Style::ClassEqualityComparison

Enforces the use of ‘Object#instance_of?` instead of class comparison for equality. `==`, `equal?`, and `eql?` custom method definitions are allowed by default. These are customizable with `AllowedMethods` option.

@safety

This cop's autocorrection is unsafe because there is no guarantee that
the constant `Foo` exists when autocorrecting `var.class.name == 'Foo'` to
`var.instance_of?(Foo)`.

@example

# bad
var.class == Date
var.class.equal?(Date)
var.class.eql?(Date)
var.class.name == 'Date'

# good
var.instance_of?(Date)

@example AllowedMethods: [‘==’, ‘equal?’, ‘eql?’] (default)

# good
def ==(other)
  self.class == other.class && name == other.name
end

def equal?(other)
  self.class.equal?(other.class) && name.equal?(other.name)
end

def eql?(other)
  self.class.eql?(other.class) && name.eql?(other.name)
end

@example AllowedPatterns: [] (default)

# bad
def eq(other)
  self.class.eq(other.class) && name.eq(other.name)
end

@example AllowedPatterns: [‘eq’]

# good
def eq(other)
  self.class.eq(other.class) && name.eq(other.name)
end