DEV Community

Cover image for Ruby Basics: Operators and Control Flow
Jessie Rohrer
Jessie Rohrer

Posted on

Ruby Basics: Operators and Control Flow

Photo by Peter Livesey on Unsplash

Let's start by going over Ruby's operators. Ruby has some interesting operators, many of which are actually Ruby methods. This means that you can overwrite what they do and use them to define custom behavior.

Ruby Operators

Logical Operators

Operator Description
< Less than
> Greater than
>= Greater than or equal to
<= Less than or equal to
== Equal
!= Does not equal
<=> Greater, equal, or less than
&& Logical and operator. If both operands are true, then the condition becomes true.
|| Logical or operator. If any of the two operands are non zero, then the condition becomes true.
! Logical not operator. Used to reverse the logical state of its operand. If a condition is true, then the logical not operator will make it false.

These operators are all Ruby methods which return a boolean value. Except for the last one, which is referred to as the 'spaceship operator'. This one returns a numerical value: 1 for greater than, 0 for equal to, and -1 for less than. The spaceship operator is useful for sorting.

Arithmetic Operators

Not much new here. Like Ruby's logical operators, arithmetic operators are also Ruby methods.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponents
% Modulo

Assignment Operators

Unlike the logical and arithmetic operators, these operators are not Ruby methods.

Ruby's basic assignment operator is the '=' sign.

a = 1
Enter fullscreen mode Exit fullscreen mode

Ruby also has combined assignment operators:

a += 10
# 11

a *= 4
# 44
Enter fullscreen mode Exit fullscreen mode

These operators are basically taking the value of the variable, doing some math with it, and then saving the result inside the original variable. This is helpful for incrementing values. You can do this with all of the arithmetic operators.

There are two other assignment operators that look similar, but behave differently. ||= and &&=.

a ||= 100 does this: If a doesn't exist or if it is false or nil, then assign 100 to it, otherwise return the value of a.

The assignment will only happen if the first variable resolves to the right value. false or nil for || and true for &&.

Shortcut Operator What is actually happening
a ||= b a || a = b
a &&= b a && a = b

Control Flow

If Statements

Ruby's if statement takes in an expression. If that expression is true, Ruby executes the block of code that follows the 'if'. If the expression is false, Ruby doesn't execute the block of code; it skips it and moves on to the 'else' or 'end', depending on what the if statment is doing.

if something is true
    do this
end
Enter fullscreen mode Exit fullscreen mode

Above, the statement simply ends. Below, an else is provided to have the program perform another action in case the if evaluates to false.

if something is true
    do this
else
    do this
end
Enter fullscreen mode Exit fullscreen mode

If/Elsif Statements

What if your logic requires more than two options? The elsif statement can add any number of alternatives to an if/else statement.

if a < b
    puts "a is less than b"
elsif a > b
    puts "a is greater than b"
else
    "a and b are equal"
end
Enter fullscreen mode Exit fullscreen mode

Unless Statements

If you want to use control flow to check if something is false rather than true, you can use an unless statement.

unless raining
    wear sunglasses
else
    bring an umbrella
end
Enter fullscreen mode Exit fullscreen mode

Case Statements

Case statements are useful if you have lots of options, or cases, you want to account for.

Keyword Description
case Starts the case statement definition. Takes the variable that you are going to work with.
when Every condition that can be matched is one 'when' statement.
else If nothing matches, then do this. (optional)
case superhero
when "Superman"
    puts "It's a bird! It's a plane!"
when "Batman"
    puts "Na na na na na na na na Batman!"
when "Flash"
    puts "Flash, ah AH!"
else 
    puts "They're here to save us!"
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)