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.
- # p014constructs.rb
- # In Ruby, nil and false evaluate to false,
- # everything else (including true, 0) means true
- # nil is an actual object
- # if else end
- var = 5
- if var > 4
- puts "Variable is greater than 4"
- puts "I can have multiple statements here"
- if var == 5
- puts "Nested if else possible"
- else
- puts "Too cool"
- end
- else
- puts "Variable is not greater than 5"
- puts "I can have multiple statements here"
- end
An example of using elsif is there in the program p015elsifex.rb as shown below:
- # p015elsifex.rb
- # elseif example
- # Original example
- puts "Hello, what's your name?"
- STDOUT.flush
- name = gets.chomp
- puts 'Hello, ' + name + '.'
- if name == 'Satish'
- puts 'What a nice name!!'
- else
- if name == 'Sunil'
- puts 'Another nice name!'
- end
- end
- # Modified example with elseif
- puts "Hello, what's your name?"
- STDOUT.flush
- name = gets.chomp
- puts 'Hello, ' + name + '.'
- if name == 'Satish'
- puts 'What a nice name!!'
- elsif name == 'Sunil'
- puts 'Another nice name!'
- end
- # Further modified
- puts "Hello, what's your name?"
- STDOUT.flush
- name = gets.chomp
- puts 'Hello, ' + name + '.'
- # || is the logical or operator
- if name == 'Satish' || name == 'Sunil'
- puts 'What a nice name!!'
- 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.
- unless ARGV.length == 2
- puts "Usage: program.rb 23 45"
- exit
- 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.
- # Loops
- var = 0
- while var < 10
- puts var
- var += 1
- 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:
- (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:
- age = 15
- # We talk about the Range class later on
- # will output teenager
- puts (13...19).include?(age) ? "teenager" : "not a teenager"
The ternary operator also can be useful for conditional assignments:
- age = 23
- person = (13...19).include?(age) ? "teenager" : "not a teenager"
- 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.
- 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.
- year = 2000
- leap = case
- when year % 400 == 0 then true
- when year % 100 == 0 then false
- else year % 4 == 0
- end
- puts leap
- # 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.
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:
- # We can determine our object's class and its unique object ID
- # NIL is synonym for nil
- puts NIL.class # NilClass
- puts nil.class # NilClass
- puts nil.object_id # 4
- puts NIL.object_id # 4
- # FALSE is synonym for false
- puts FALSE.class # FalseClass
- puts false.class # FalseClass
- puts false.object_id # 0
- puts FALSE.object_id # 0