A conditional modifier places if or unless after a statement, so Ruby executes that statement only when the condition permits it. For a simple condition, this avoids an enclosing end. section 6.1.1
Postfix if and unless
puts "Big number!" if x > 100 is equivalent to a multiline if/end form. The inverse style, puts "Big number!" unless x <= 100, expresses the same kind of one-statement decision. section 6.1.1
Use conditional modifiers when:
- The condition controls one straightforward statement. section 6.1.1
- Avoid them when branching needs else or elsif, or when the trailing statement becomes long and difficult to read. section 6.1.1
if condition
# code here, executed if, and only if, condition is true
end
Compare the postfix examples with the ordinary if/end structure to see how the modifier removes the enclosing block. section 6.1.1
