Class: RuboCop::Cop::Style::DisallowTernary

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/disallow_ternary.rb

Overview

Forbids the ternary conditional. ? and : are punctuation that already mean other things in Ruby (valid?, :symbol), so the reader disambiguates characters before seeing the branch — and the ternary hides a branch inside an expression, where skimming misses it. Inside a hash or an argument list, extract a named local and branch on it.

Examples:

# bad
status = saved ? :applied : :failed

# good
if saved
  status = :applied
else
  status = :failed
end

Constant Summary collapse

MSG =
'Do not use the ternary conditional. Use an explicit `if`/`else` instead.'

Instance Method Summary collapse

Instance Method Details

#on_if(node) ⇒ Object



28
29
30
# File 'lib/rubocop/cop/style/disallow_ternary.rb', line 28

def on_if(node)
  add_offense(node) if node.ternary?
end