Simple Constructs in Ruby
Today, we shall explore some very simple constructs available in Ruby. The example below (Constructs.rb) illustrates the if else end construct.
Some common conditional operators are: ==, != >=, <=, >, <
Loops like the while are available. Again, the example below illustrates the same.
I shall talk about another construct the do end when we discuss arrays.
Technorati Tags: Simple Constructs in Ruby
Blogs linking to this article
# if else endAn example of using elsif is there in the program ElsIfEx.rb
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
Some common conditional operators are: ==, != >=, <=, >, <
Loops like the while are available. Again, the example below illustrates the same.
# Loops
var = 0
while var < 10
puts var.to_s
var += 1
end
I shall talk about another construct the do end when we discuss arrays.
First Post | Previous | Next
Technorati Tags: Simple Constructs in Ruby
Blogs linking to this article
4 Comments:
Robert, you can post your program as a comment to my post
Hi Sir,
I am confused why,
puts 'I can have multiple statements here'
and
puts 'I can not have multiple statements here'
Does the comments imply that I really can not have multiple statements at the second place? I tried and it worked or am I missing something?
Anish, I am sorry the text in the puts statement was confusing. I have changed it. We can have multiple statements both places.
Sandip, send me your code.
Post a Comment
<< Home