Control Flow in Ruby
Published: 2024-01-18 16:26:24

_Codecademy Cheatsheet_

**If**

```
if 1 < 2
  print "I'm getting printed because one is less than two!"
end

```
Ruby doesn’t care about whitespace (spaces and blank lines), so the indentation of the print statement isn’t necessary. However, it’s a convention that Rubyists (Ruby enthusiasts) follow, so it’s good to get in the habit now. The block of code following an if should be indented two spaces.

When you’re done with your `if`, you have to tell Ruby by typing `end.`

---

**Else**

The partner to the `if` statement is the `else `statement. An `if/else `statement says to Ruby: “If this expression is true, run this code block; otherwise, run the code after the `else` statement.” 

```
if 1 > 2
  print "I won't get printed because one is less than two."
else
  print "That means I'll get printed!"
end

```

---

**Elsif**

```
if x < y  # Assumes x and y are defined
  puts "x is less than y!"
elsif x > y
  puts "x is greater than y!"
else
  puts "x equals y!"
end

```

---

**Unless**

Sometimes you want to use control flow to check if something is false, rather than if it’s true. You could reverse your if/else, but Ruby will do you one better: it will let you use an unless statement.

Let’s say you don’t want to eat unless you’re hungry. That is, while you’re not hungry, you write programs, but if you are hungry, you eat. You might write that program in Ruby like this:

```
unless hungry
  # Write some sweet programs
else
  # Have some noms
end

```

---

**Equal or Not**

In Ruby, we assign values to variables using `=`, the assignment operator. But if we’ve already used `=` for assignment, how do we check to see if two things are equal? Well, we use `==`, which is a comparator (also called a relational operator). `==` means “is equal to.” When you type

```
x = 2
y = 2
if x == y
  print "x and y are equal!"
end

```
you’re saying: “if x equals y, print ‘x and y are equal!’” You can also check to see if two values are not equal using the `!=` comparator.

---

**And**

Comparators aren’t the only operators available to you in Ruby. You can also use logical or boolean operators. Ruby has three: and `(&&)`, or `(||)`, and not (`!`). Boolean operators result in boolean values: true or false.

The boolean operator and, `&&`, only results in true when both expression on either side of && are true. Here’s how && works:

```
true && true # => true
true && false # => false
false && true # => false
false && false # => false

```

---

**Or**

Ruby also has the or operator (||). Ruby’s || is called an inclusive or because it evaluates to true when one or the other or both expressions are true. Check it out:

```
true || true # => true
true || false # => true
false || true # => true
false || false # => false


```

---

**Not**

Finally, Ruby has the boolean operator not (!). ! makes true values false, and vice-versa.

```
!true # => false
!false # => true

```
**Combining Boolean Operators**

```
(x && (y || w)) && z
```

---

_Daffy Duckified_

```
print "Pleathe enter a thtring: " 
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
  user_input.gsub!(/s/, "th")
else
  puts "There are no 's's in your string."
end

puts "Your new thtring is #{user_input}."
```
For the if half of our branch, we want to check whether the user’s input contains an "s".

```
if string_to_check.include? "substring"

```
When we find "s", we want Ruby to replace every instance of "s" it finds with "th". We can do this with the `.gsub!` method, which stands for **g**lobal **sub**stitution.

The syntax looks like this:

```
string_to_change.gsub!(/s/, "th")

```