Ruby Basics: Operators and Control Flow
Tags: ruby, beginners, basics, webdev
Published: 2021-04-11 19:53:20

Photo by <a href="https://unsplash.com/@plivesey?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Peter Livesey</a> on <a href="https://unsplash.com/s/photos/flow?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
  

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. |
|   &#x7c;&#x7c; |   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. 

```Ruby
a = 1
```

Ruby also has combined assignment operators:

```Ruby
a += 10
# 11

a *= 4
# 44
```

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 &#x7c;&#x7c;= b | a &#x7c;&#x7c; 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. 

```Ruby
if something is true
    do this
end
```
Above, the statement simply ends. Below, an else is provided to have the program perform another action in case the if evaluates to false.

```Ruby
if something is true
    do this
else
    do this
end
```

### 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.

```Ruby
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
```

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

```Ruby
unless raining
    wear sunglasses
else
    bring an umbrella
end
```

### 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) |


```Ruby
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
```