w3resource

Ruby if, else and unless statement

if, else and unless

Ruby has a variety of ways to control execution that is pretty common to other modern languages. All the expressions described here return a value.

Here, we have explained if Expression, Ternary if, unless Expression, Modifier if and unless and case Expression

For the tests in these control expressions :

  • nil and false are false-values
  • true and any other object are true-values

Ruby: if Expression

The if expressions execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

Syntax:

if conditional [then]
	  code...
[elsif conditional [then]
	  code...]...
[else
	  code...]
end

The simplest if expression has following two parts :

  • a "test" expression
  • a “then” expression.

If the "test" expression evaluates to a true then the "then" expression is evaluated.

Example:

x = 10
if x > 8 then
puts "x is greater than 8"
end

Output:

H:\>ruby abc.rb
x is greater than 8

The then is optional:

x = 10
if x > 8
puts "x is greater than 8"
end

Output:

H:\>ruby abc.rb
x is greater than 8

You can also add an else expression. If the test does not evaluate to true, then the else expression will be executed :

Example:

x = 10
if x > 18
  puts "x is greater than 18"
else
  puts "x is not greater than 18"
end

Output:

H:\>ruby abc.rb
x is not greater than 18

You can add an arbitrary number of extra tests to an if expression using elsif. An elsif executes when all tests above the elsif are false.

Example:

x = 100
if x == 1
  puts "x is One."
elsif x == 100
  puts "x is Hundred."
else
  puts "x has different value."
end

Output:

H:\>ruby abc.rb
x is Hundred.

Ruby: Ternary if

You may also write a if-then-else expression using ? and :

x = 10
(x > 10) ? puts("x is greater than 18") : puts("x is not greater than 18")

Is the same as this if expression :

x = 10
if x > 18
  puts "x is greater than 18"
else
  puts "x is not greater than 18"
end

Ruby: unless Statement:

The unless expression is the opposite of the if expression. If the value is false the "then" expression is executed :

Syntax:

unless conditional [then]
   code
[else
   code ]
end

Example:

The following code prints nothing as the value of x is 1.

x = 1
unless x > 0
 puts "x is less than 0"
end

The following code will print "x is greater than 0".

Example:

x = 1
unless x > 0
 puts "x is less than 0"
else   
 puts "x is greater than 0"
end

Ruby: Modifier if and unless

You can use if and unless to modify an expression. When used as a modifier the left-hand side is the "then" expression and the right-hand side is the "test" expression:

Following code will print 1.

x = 0
x += 1 if x.zero?
print(x)
x = 0
x += 1 unless x.zero?
print(x)

The above code will print 0.

Previous: Ruby Dot And Double Colon Operators
Next: Ruby Case Statement



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby/ruby-if-else-unless.php