Ruby - Statement if statement

Introduction

Consider the following code:

Demo

age = 10 
puts "You're too young to use this system" if age < 18

Result

If the value of age is under 18, the string is printed to the screen.

The following code is equivalent:

Demo

age = 10 
if age < 18 # from   ww w  . j a  v  a2 s .c  om
 puts "You're too young to use this system"  
end

Result

To put any number of lines of code in between the if statement and the end line:

Demo

age = 10 
if age < 18 #  w w  w .  j  av  a2  s  .c o m
   puts "You're too young to use this system" 
   puts "So we're going to exit your program now" 
   exit 
end

Result

Related Topics