Home > database >  Rubocop Lint/DuplicateMethods -- which duplicate should I keep?
Rubocop Lint/DuplicateMethods -- which duplicate should I keep?

Time:03-23

Rubocop reports:

app/models/transformer.rb:8:3: W: Lint/DuplicateMethods: Method Painter#last_color 
  is defined at both app/models/painter.rb:2 and app/models/painter.rb:5.

Painter class looks like:

class Painter
  attr_accessor :last_color
  alias last_color recent_color

  def last_color
    colors.last
  end
end

Which one actually gets used? The accessor, alias, or method?

CodePudding user response:

The defined method takes priority over the alias and accessor.

The accessor and alias should be deleted, leaving only the method defined in the class.

CodePudding user response:

If you're going to define a public method last_color then adding an attr_accessor is redundant.

So if you're asking which one would get executed when you run Painter.new.last_color then the answer would be your defined method.

You don't need to get rid of the alias (assuming you need it for some reason).

  • Related