Computer programs are useful for automating processes because they can be programmed to perform different actions for different scenarios. Take email, for example: if you try to send an email that has no recipient, then the program should ask you to provide one; otherwise, if there is an email but no subject line, then the program should ask if you're sure you don't want to provide a subject; otherwise, if there is an email and a subject (or you're sure you don't want one), then send the email with no questions asked.
In this lesson, we will learn about how we can use logic to control the actions of our program.
Imagine that we have been tasked to write an ATM (automated teller machine) program where users have three options:
- Check balance
- Withdraw funds
- Cancel
If the first option (1
) is selected, then we should display to the the user via puts
what their current account balance is; otherwise, if the second option (2
) is selected, then we should prompt them for how much they would like to withdraw and then perform the widthdrawal; otherwise, if the third option (3
) is selected, then the program exits; otherwise, if anything else is selected, then the program ask them to please select only the available options. Here is an outline of this same logic in code:
puts %{
1: Check balance
2: Withdraw funds
3: Cancel
}
print ">> "
selected = gets.chomp
if selected == 1
check_balance
elsif selected == 2
widthdraw_funds
elsif selected == 3
exit_atm
else
puts "Please only select 1, 2, or 3"
end
Any if
statement that has code within it must be terminated by an end
, as shown on the last line above. If there are other conditions that are not handled by the first if
, you can use elsif
to specify another condition, or you can use else
to handle anything not specified in the previous if
and elsif
s.
Tip: If you have a single line that should be conditionally evaluated, you can append if
to the end of that line! Here's how:
first_name = "Jane"
middle_name = ""
last_name = "Smith"
name = first_name
name += " #{middle_name}" if middle_name.length > 0
name += " #{last_name}"
name # => "Jane Smith"
When we find ourselves using an if
statement with many elsif
that are comparing against the same variable or expression, then we can simplify this a tad more by using a case
statement. Here is the if
from the previous section simplified in to a case
statement:
if selected == 1
check_balance
elsif selected == 2
widthdraw_funds
elsif selected == 3
exit_atm
else
puts "Please only select 1, 2, or 3"
end
# can be simplified with case
case selected
when 1
check_balance
when 2
widthdraw_funds
when 3
exit_atm
else
puts "Please only select 1, 2, or 3"
end
# or if you want to do each on one line
case selected
when 1 then check_balance
when 2 then widthdraw_funds
when 3 then exit_atm
else
puts "Please only select 1, 2, or 3"
end
We often times find ourselves performing what we'll call "negative" logic that consists of using an if
with !=
to say, "If something is not equal to something else." If you are using this on one-liners or without an else
section to your conditional, consider using an unless
statement to keep things "positive":
withdraw_funds if check_balance != 0
# or
withdraw_funds unless check_balance == 0
This is also useful for multiline code that must be executed:
unless check_balance == 0
withdraw_funds
# other code goes here
end
But beware using an else
with unless
, for it's fairly easy to get positive/negative logic mixed around in your head! If you find yourself needing an else
, consider sticking with an if
.
If you have a very simple if
/ else
statement that looks like this,
if 2 + 2 == 5
puts "1984 got it right"
else
puts "Math wins!"
end
then you can consider using a ternary operator to place all of this on one line. But make sure to use this sparingly, for it can become hard to reason about if not done simply! Here is the same conditional, but this time using a ternary:
2 + 2 == 5 ? puts "1984 got it right" : puts "Math wins!"
Let's break this down. The 2 + 2 == 5
part is the condition that will the truthy or falsy. If the condition is truthy, then the code that is between the ?
and :
, puts "1984 got it right"
, will be executed; if it is falsy, then the code after the :
, puts "Math wins!"
, will be executed. Here is a way to read this out loud:
# pseudo-code
2 + 2 == 5 [if so] puts "1984 got it right" [otherwise] puts "Math wins!"