In addition to the if statement, there is also a method called unless for conditional bifurcation in Ruby. If the conditional expression is true, the if statement is executed, and if it is false, the process described after else is executed.
if conditional expression
Processing to be executed when the conditional expression is true
else
Processing to be executed when the conditional expression is false
end
In the unless statement, the process is executed when the conditional expression is false, and when it is true, the process described after else is executed.
unless conditional expression
Processing to be executed when the conditional expression is false
else
Processing to be executed when the conditional expression is true
end
For example
if a + b > 0
puts "The calculation result is greater than 0"
end
If you write the if statement of, using the unless statement,
unless a + b <= 0
puts "The calculation result is greater than 0"
end
Since 0 is not included in the if statement, setting it to 0 or more in the unless statement has the same meaning.
Recommended Posts