class RuboCop::Cop::Style::OptionHash

This cop checks for options hashes and discourages them if the current Ruby version supports keyword arguments.

@example

Instead of:

def fry(options = {})
  temperature = options.fetch(:temperature, 300)
  ...
end

Prefer:

def fry(temperature: 300)
  ...
end

Constants

MSG

Public Instance Methods

on_args(node) click to toggle source
# File lib/rubocop/cop/style/option_hash.rb, line 24
def on_args(node)
  *_but_last, last_arg = *node

  return unless last_arg && last_arg.optarg_type?

  arg, default_value = *last_arg

  return unless default_value.hash_type? && default_value.pairs.empty?
  return unless suspicious_name?(arg)

  add_offense(last_arg)
end

Private Instance Methods

suspicious_name?(arg_name) click to toggle source
# File lib/rubocop/cop/style/option_hash.rb, line 39
def suspicious_name?(arg_name)
  cop_config.key?('SuspiciousParamNames') &&
    cop_config['SuspiciousParamNames'].include?(arg_name.to_s)
end