Ruby 2 Simple Constructs

Let's explore some very simple constructs available in Ruby. The example below p014constructs.rb illustrates the if else end construct. By the Ruby convention, if and while do not require parenthesis.
  1. # p014constructs.rb  
  2. # In Ruby, nil and false evaluate to false,  
  3. # everything else (including true, 0) means true  
  4. # nil is an actual object  
  5. # if else end  
  6. var = 5  
  7. if var > 4  
  8.   puts "Variable is greater than 4"  
  9.   puts "I can have multiple statements here"  
  10.   if var == 5  
  11.     puts "Nested if else possible"  
  12.   else  
  13.     puts "Too cool"  
  14.   end  
  15. else  
  16.   puts "Variable is not greater than 5"  
  17.   puts "I can have multiple statements here"  
  18. end  
An example of using elsif is there in the program p015elsifex.rb as shown below:
  1. # p015elsifex.rb  
  2. # elseif example  
  3.   
  4. # Original example  
  5. puts "Hello, what's your name?"  
  6. STDOUT.flush  
  7. name = gets.chomp  
  8. puts 'Hello, ' + name + '.' 
  9.  
  10. if name == 'Satish' 
  11.   puts 'What a nice name!!' 
  12. else 
  13.   if name == 'Sunil' 
  14.     puts 'Another nice name!' 
  15.   end 
  16. end 
  17.  
  18. # Modified example with elseif 
  19. puts "Hello, what's your name?"  
  20. STDOUT.flush  
  21. name = gets.chomp  
  22. puts 'Hello, ' + name + '.'  
  23.   
  24. if name == 'Satish'  
  25.   puts 'What a nice name!!'  
  26. elsif name == 'Sunil'  
  27.   puts 'Another nice name!'  
  28. end  
  29.   
  30. # Further modified  
  31. puts "Hello, what's your name?"  
  32. STDOUT.flush  
  33. name = gets.chomp  
  34. puts 'Hello, ' + name + '.' 
  35.  
  36. # || is the logical or operator 
  37. if name == 'Satish' || name == 'Sunil' 
  38.   puts 'What a nice name!!'  
  39. end  
Some common conditional operators are: ==, != >=, <=, >, <
unless, as a statement or a modifier, is the opposite of if: it executes code only if an associated expression evaluates to false or nil. Ruby's unless construct begins with unless and ends with end. The body is the text between the two.
  1. unless ARGV.length == 2  
  2.   puts "Usage: program.rb 23 45"  
  3.   exit  
  4. end  
In the above program, the body is executed unless the number of elements in the array is equal to 2 (meaning that both arguments were given). The method Kernel.exit terminates your program, returning a status value to the operating system.
Loops like the while loop are available. Again, the example below illustrates the same.
  1. # Loops  
  2. var = 0  
  3. while var < 10  
  4.   puts var  
  5.   var += 1  
  6. end  

Conditional ?:

As a concise alternative to simple if/else statements we can use the conditional or ternary ?: operator. It is the only ternary operator (three operands) in Ruby. It has the following basic structure:
  1. (condition) ? (result if condition is true) : (result if condition is false)  
The first operand appears before the question mark. The second operand appears between the question mark and the colon. An the third operand appears after the colon. The question mark must appear on the same line as the first argument and the colon must appear on the same line as the second argument. The ?: operator always evaluates its first operand. If the first operand is anything other than false or nil, the value of the expression is the value of the second operand. Otherwise, if the first operand is false or nil, then the value of the expression is the value of the third operand. The ?: operator acts like a compact if/then/else statement. Let's look at an example:
  1. age = 15  
  2. # We talk about the Range class later on  
  3. # will output teenager  
  4. puts (13...19).include?(age) ? "teenager" : "not a teenager"  
The ternary operator also can be useful for conditional assignments:
  1. age = 23  
  2. person = (13...19).include?(age) ? "teenager" : "not a teenager"  
  3. puts person # => "not a teenager"  

Statement modifiers

Ruby statement modifiers are a useful shortcut if the body of an if or unless statement is just a single expression. Simply write the expression, followed by if or unless and the condition. For example, here's a simple if statement.
  1. puts "Enrollments will now Stop" if participants > 2500  

Case Expressions

This form is fairly close to a series of if statements: it lets you list a series of conditions and execute a statement corresponding to the first one that's true. For example, leap years must be divisible by 400, or divisible by 4 and not by 100. Also, remember that case returns the value of the last expression executed.
  1. year = 2000  
  2. leap = case  
  3.        when year % 400 == 0 then true  
  4.        when year % 100 == 0 then false  
  5.        else year % 4   == 0  
  6.        end  
  7. puts leap  
  8. # output is: true  

nil is an Object

In Ruby, nil is an actual object. You can call methods on nil, just like any other object. You can add methods to nil, just like any other object.

In Ruby, nil and false evaluate to false, everything else (including true, 0) means true.

Difference between FALSE and NIL

Though we still have to talk about classes, nevertheless here is some additional information for you. nil and false are not the same things. Both have a false value and also remember that everything in Ruby is an object. See the following program:
  1. # We can determine our object's class and its unique object ID  
  2. # NIL is synonym for nil  
  3. puts NIL.class # NilClass  
  4. puts nil.class # NilClass  
  5. puts nil.object_id # 4  
  6. puts NIL.object_id # 4  
  7.   
  8. # FALSE is synonym for false  
  9. puts FALSE.class # FalseClass  
  10. puts false.class # FalseClass  
  11. puts false.object_id # 0  
  12. puts FALSE.object_id # 0