class RuboCop::Cop::Style::WhileUntilModifier

Checks for while and until statements that would fit on one line if written as a modifier while/until. The maximum line length is configurable.

Constants

MSG

Public Instance Methods

on_until(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 17
def on_until(node)
  check(node)
end
on_while(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 13
def on_while(node)
  check(node)
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 23
def autocorrect(node)
  oneline = "#{node.body.source} #{node.keyword} " \
            "#{node.condition.source}"

  lambda do |corrector|
    corrector.replace(node.source_range, oneline)
  end
end
check(node) click to toggle source
# File lib/rubocop/cop/style/while_until_modifier.rb, line 32
def check(node)
  return unless node.multiline? && single_line_as_modifier?(node)

  add_offense(node, location: :keyword,
                    message: format(MSG, node.keyword))
end