An else block in an if expression contains code that is called when the if conditional evaluates to false.
age = 15
if age > 18
puts “Too young”
# outputs “Too young”
The elsif (short for else if) block is useful when you want to test multiple conditions.
A series of if elsif expressions can have a final else block, which is called if none of the if or elsif expressions are true.
num = 8
if num == 3
puts “Number is 3”
elsif num == 10
puts “Number is 10”
elsif num == 7
puts “Number is 7”
puts “Not found”
The unless expression is the opposite of an if expression. It executes code when a conditional is false.
a = 42
unless a < 10
# outputs “Yes”
You can use an else block with the unless, just like you did with the if expression. The end keyword is also required to close the block.
The if and unless modifiers can also be used to execute code.
a = 42
puts “Yes” if a > 10
puts “Yes” unless a < 10
This code before the if executes only if the condition evaluates to true.
The code before the unless executes only if the condition is false.
CLICK ON THE BUTTON BELOW TO GO TO THE RUBY MAIN COURSE PAGE.
© License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT