Search code examples
rubyrubocop

RuboCop complains when using 'Hash.new'


RuboCop complains when I use Hash.new, and suggests that I instead use a hash literal. Is there a way to make RuboCop ignore uses of Hash.new? More specifically, can I edit my .rubocop.yml configuration to allow the use of Hash.new without raising any complaints?


Solution

  • You can disable the Rubocop::Cop::Style::EmptyLiteral cop within the rubocop.yml file:

    # .rubocop.yml
    Style:
      EmptyLiteral: false
    

    Or if you want just to ignore a certain line:

    hsh = Hash.new # rubocop:disable Style/EmptyLiteral