O'Reilly logo

Learning Sinatra by Manoj Sehrawat, Sudeep Agarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Control structures in Ruby

Ruby has various control structures, many of which you will be familiar with. A control structure executes statements until the condition is not false. Any value that is not nil or false is treated as true. For some of you who are comfortable with C++ or Java, you may sometimes find it confusing that 0 and empty string are not false.

if-elsif-else-end

The if-elsif-else-end control structure in Ruby is exactly the same as that in other programming languages:

if <condition>
  <statements>
elsif <condition>
  <statements>
else
  <statements>
end

Here is an example:

a = 1 b = 2 if a > b puts "The value of a is greater than the value of b" elsif a < b puts "The value of a is less than the value of b" else puts "The value of a is equal ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required