Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::MethodDefParentheses

This cops checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 32
def autocorrect(node)
  @corrections << lambda do |corrector|
    if style == :require_parentheses
      args_expr = args_node(node).loc.expression
      args_with_space = range_with_surrounding_space(args_expr, :left)
      just_space = Parser::Source::Range.new(args_expr.source_buffer,
                                             args_with_space.begin_pos,
                                             args_expr.begin_pos)
      corrector.replace(just_space, '(')
      corrector.insert_after(args_expr, ')')
    elsif style == :require_no_parentheses
      corrector.replace(node.loc.begin, ' ')
      corrector.remove(node.loc.end)
    end
  end
end
check(node, _method_name, args, _body) click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 11
def check(node, _method_name, args, _body)
  if style == :require_parentheses
    if arguments?(args) && !parentheses?(args)
      add_offense(node,
                  args.loc.expression,
                  'Use def with parentheses when there are '                            'parameters.') do
        opposite_style_detected
      end
    else
      correct_style_detected
    end
  elsif parentheses?(args)
    add_offense(args, :expression, 'Use def without parentheses.') do
      opposite_style_detected
    end
  else
    correct_style_detected
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.