class RuboCop::Cop::Style::SingleLineMethods

This cop checks for single-line method definitions that contain a body. It will accept single-line methods with no body.

@example

# bad
def some_method; body end
def link_to(url); {:name => url}; end
def @table.columns; super; end

# good
def no_op; end
def self.resource_class=(klass); end
def @table.columns; end

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/single_line_methods.rb, line 33
def autocorrect(node)
  lambda do |corrector|
    each_part(node.body) do |part|
      LineBreakCorrector.break_line_before(
        range: part, node: node, corrector: corrector,
        configured_width: configured_indentation_width
      )
    end

    LineBreakCorrector.break_line_before(
      range: node.loc.end, node: node, corrector: corrector,
      indent_steps: 0, configured_width: configured_indentation_width
    )

    move_comment(node, corrector)
  end
end
on_def(node) click to toggle source
# File lib/rubocop/cop/style/single_line_methods.rb, line 25
def on_def(node)
  return unless node.single_line?
  return if allow_empty? && !node.body

  add_offense(node)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

allow_empty?() click to toggle source
# File lib/rubocop/cop/style/single_line_methods.rb, line 53
def allow_empty?
  cop_config['AllowIfMethodIsEmpty']
end
each_part(body) { |source_range| ... } click to toggle source
# File lib/rubocop/cop/style/single_line_methods.rb, line 57
def each_part(body)
  return unless body

  if body.begin_type?
    body.each_child_node { |part| yield part.source_range }
  else
    yield body.source_range
  end
end
move_comment(node, corrector) click to toggle source
# File lib/rubocop/cop/style/single_line_methods.rb, line 67
def move_comment(node, corrector)
  LineBreakCorrector.move_comment(
    eol_comment: end_of_line_comment(node.source_range.line),
    node: node, corrector: corrector
  )
end