Ruby - if and unless Modifiers

Introduction

You may recall the alternative syntax for while loops mentioned. Instead of writing this:

while tired do sleep end 

you can write this:

sleep while tired 

This alternative syntax is called a while modifier.

Ruby has if and unless modifiers too. Here are a few examples:

sleep if tired 

begin  
   sleep  
   snore 
end if tired 

sleep unless not tired 

begin  
   sleep  
   snore 
end unless not tired 

You might, for example, use the if statement as follows:

puts( "somevar = #{somevar}" ) if DEBUG  

Related Topic