Ruby if
Statement
Ruby provides several common conditional structures. Here, we will explain all the conditional statements and available modifiers in Ruby.
Ruby if...else Statement
Syntax
if conditional [then]
code...
[elsif conditional [then]
code...]...
[else
code...]
end
The if expression is used for conditional execution. The values false and nil are false, and everything else is true. Note that Ruby uses elsif
, not else if
or elif
.
If the conditional is true, the code is executed. If the conditional is not true, the code specified in the else clause is executed.
Usually, we omit the reserved word then
. If you want to write the entire if expression in one line, you must separate the condition and the code block with then
, as shown below:
if a == 4 then a = 7 end
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
x = 1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x != 0
puts "x is 1"
else
puts "Cannot determine the value of x"
end
Output:
x is 1
Ruby if Modifier
Syntax
code if condition
The if modifier means that the code on the left is executed if the condition on the right is true.
Example
#!/usr/bin/ruby
$debug = 1
print "debug\n" if $debug
Output:
debug
Ruby unless Statement
Syntax
unless conditional [then]
code
[else
code ]
end
The unless expression is the opposite of the if expression. If the conditional is false, the code is executed. If the conditional is true, the code specified in the else clause is executed.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
x = 1
unless x > 2
puts "x is less than or equal to 2"
else
puts "x is greater than 2"
end
Output:
x is less than or equal to 2
Ruby unless Modifier
Syntax
code unless conditional
The code is executed if the conditional is false.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$var = 1
print "1 -- This line is output\n" if $var
print "2 -- This line is not output\n" unless $var
$var = false
print "3 -- This line is output\n" unless $var
Output:
1 -- This line is output
3 -- This line is output
Ruby case Statement
Syntax
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
The case statement first matches an expression and then branches based on the match result.
It uses the ===
operator to compare the expression specified in the when
clause. If it matches, the code in the when
block is executed.
Usually, we omit the reserved word then
. If you want to write the entire when
expression in one line, you must separate the condition and the code block with then
, as shown below:
when a == 4 then a = 7 end
Thus:
case expr0
when expr1, expr2
stmt1
when expr3, expr4
stmt2
else
stmt3
end
Is essentially similar to:
_tmp = expr0
if expr1 === _tmp || expr2 === _tmp
stmt1
elsif expr3 === _tmp || expr4 === _tmp
stmt2
else
stmt3
end
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$age = 5
case $age
when 0..2
puts "Baby"
when 3..6
puts "Child"
when 7..12
puts "Child"
when 13..18
puts "Teenager"
else
puts "Other age group"
end
Output:
Child
When the "expression" part of the case statement is omitted, it evaluates the first when
condition that is true.
foo = false
bar = true
quu = false
case
when foo then puts 'foo is true'
when bar then puts 'bar is true'
when quu then puts 'quu is true'
end
# Outputs "bar is true"