DEV Community

Aizat Ibraimova
Aizat Ibraimova

Posted on • Updated on

Control Flow in Ruby

Codecademy Cheatsheet

If

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

Enter fullscreen mode Exit fullscreen mode

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 elsestatement. An if/elsestatement 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Not

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

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

Enter fullscreen mode Exit fullscreen mode

Combining Boolean Operators

(x && (y || w)) && z
Enter fullscreen mode Exit fullscreen mode

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}."
Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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 global substitution.

The syntax looks like this:

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

Enter fullscreen mode Exit fullscreen mode

Top comments (0)