Parent

Methods

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::GuardClause

Use a guard clause instead of wrapping the code inside a conditional expression

@example

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/guard_clause.rb, line 33
def on_def(node)
  _, _, body = *node
  return unless body

  if if?(body)
    check_if_node(body)
  elsif body.type == :begin
    expressions = *body
    last_expr = expressions.last

    check_if_node(last_expr) if if?(last_expr)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.