Ruby is one of the programming languages you can use almost everywhere. It is also simple and very easy to learn. This makes it a very good choice if you want to learn new programming language, especially if it is your first programming language. Will you give Ruby a chance? If so, you are on the right place. This series will teach you all you need to learn this programming language.

Getting Started With Ruby the Easy Way Part 1 (Comments, Variables, Strings).

Getting Started With Ruby the Easy Way Part 2 (Data Types Pt1).

Getting Started With Ruby the Easy Way Part 3 (Data Types Pt2, Control Flow Pt1).

Getting Started With Ruby the Easy Way Part 5 (Control Flow Pt3).

Getting Started With Ruby the Easy Way Part 6 (Methods).

Getting Started With Ruby the Easy Way Part 7 (Recursion, Scope, OOP Pt1).

Getting Started With Ruby the Easy Way Part 8 (OOP Pt2).

Getting Started With Ruby the Easy Way Part 9 (OOP Pt3).

Getting Started With Ruby the Easy Way Part 10 (OOP Pt4 and Beyond).

Control flow Pt2

The if statement is not the only tool you can use to control how Ruby executes your code. The palette is much richer. Let’s take a look what other control flow tools, or structures, you can use in Ruby.

Ternary operators

In the previous part, you’ve learned about if statements. The if statement can is very useful and powerful Although it is still very simple. However, you can take its simplicity to another level, and make your code shorter, if you want. How? You can achieve this by using something called ternary operator. Ternary operator does the same job as if statement.

It evaluates if some specific expression, or condition, is true of false. Then, it returns one value if the expression is true, and another value if the expression is false. You can think about it as a shorthand for if statement. Or, its compact version. The format of ternary operator is following one-liner: conditional ? true : false. This can be better illustrated on example.

Let’s say that you want to ask the user for a number between zero and ten. Then, you want to print a short message based on this input. Below are two examples. One uses if statement. The second uses ternary operator. As you will see, on the example below, the ternary operator example is much shorter while the result is the same. When is it better to use ternary operator instead of if statement?

Ruby developers usually use ternary operator where conditionals unnecessarily long. The code below can be a good example. Another way to use it is for variable assignment, when you want to quickly select between two values. As a rule of thumb, use this operator to select between two values with a simple conditional. Otherwise, if you are working with something more complex, if statement will be a better choice.

###
## If statement example
###
puts "Please enter a number between 0 and 10: "

## Ask user for input and convert it to an integer
i = gets.to_i

## Print message
if i > 5
  puts "The number you chose is greater than 5."
else
  puts "The number you chose is less than or equal to 5."
end

###
## Ternary operator example
###
puts "Please enter a number between 0 and 10: "

## Ask user for input and convert it to an integer
i = gets.to_i

## Print message using ternary operator
puts "The number you chose " + (i > 5 ? "is greater than 5." : "is less than or equal to 5.")

###
## Ternary operator and variable assignment
###
b = 5

x = b > 10 ? "bigger" : "smaller"
## the "b > 10" is the expression, or condition
## "bigger" : "smaller" are values when expression is true and false

Side note: In the example above, the one in which you ask for a number, the ternary operator is in parentheses. This is not necessary. Here, parentheses ensure that the ternary operator will not interfere with the string concatenation operators (_+_) surrounding it.

#ruby #programming #design development

Getting Started With Ruby the Easy Way Pt4
1.60 GEEK