Exercise 28: Boolean Practice
The logic combinations you learned from the last exercise are called "Boolean" logic expressions. Boolean logic is used everywhere in programming. It is a fundamental part of computation, and knowing them very well is akin to knowing your scales in music.
In this exercise you will take the logic exercises you memorized and start trying them out in Ruby. Take each of these logic problems and write what you think the answer will be. In each case it will be either true or false. Once you have the answers written down, you will start Ruby in your terminal and type each logic problem in to confirm your answers.
- true && true
- false && true
- 1 == 1 && 2 == 1
- "test" == "test"
- 1 == 1 || 2 != 1
- true && 1 == 1
- false && 0 != 0
- true || 1 == 1
- "test" == "testing"
- 1 != 0 && 2 == 1
- "test" != "testing"
- "test" == 1
- !(true && false)
- !(1 == 1 && 0 != 1)
- !(10 == 1 || 1000 == 1000)
- !(1 != 10 || 3 == 4)
- !("testing" == "testing" && "Zed" == "Cool Guy")
- 1 == 1 && (!("testing" == 1 || 1 == 0))
- "chunky" == "bacon" && (!(3 == 4 || 3 == 3))
- 3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun"))
I will also give you a trick to help you figure out the more complicated ones toward the end.
Whenever you see these Boolean logic statements, you can solve them easily by this simple process:
- Find an equality test (== or !=) and replace it with its truth.
- Find each &&/|| inside parentheses and solve those first.
- Find each ! and invert it.
- Find any remaining &&/|| and solve it.
- When you are done you should have true or false.
I will demonstrate with a variation on #20:
3 != 4 && !("testing" != "test" || "Ruby" == "Ruby")
Here's me going through each of the steps and showing you the translation until I've boiled it down to a single result:
Solve each equality test:
3 != 4 is true: true && !("testing" != "test" || "Ruby" == "Ruby")
"testing" != "test" is true: true && !(true || "Ruby" == "Ruby")
"Ruby" == "Ruby": true && !(true || true)
Find each &&/|| in parentheses ():
(true || true) is true: true && !(true)
Find each ! and invert it:
!(true) is false: true && false
Find any remaining &&/|| and solve them:
true && false is false
With that we're done and know the result is false.
Warning
The more complicated ones may seem very hard at first. You should be able to take a good first stab at solving them, but do not get discouraged. I'm just getting you primed for more of these "logic gymnastics" so that later cool stuff is much easier. Just stick with it, and keep track of what you get wrong, but do not worry that it's not getting in your head quite yet. It'll come.
What You Should See
After you have tried to guess at these, this is what your session with Ruby might look like:
$ irb irb(main):001:0> true && true => true irb(main):002:0> 1 == 1 && 2 == 2 => true irb(main):003:0>
Study Drills
- There are a lot of operators in Ruby similar to != and ==. Try to find as many "equality operators" as you can. They should be like < or <=.
- Write out the names of each of these equality operators. For example, I call != "not equal."
- Play with Ruby by typing out new Boolean operators, and before you press Enter try to shout out what it is. Do not think about it. Shout the first thing that comes to mind. Write it down, then press Enter, and keep track of how many you get right and wrong.
- Throw away the piece of paper from 3 so you do not accidentally try to use it later.
Common Student Questions
- Why does "test" && "test" return "test" or 1 && 1 return 1 instead of true?
- Ruby and many languages like to return one of the operands to their Boolean expressions rather than just true or false. This means that if you did false && 1 you get the first operand (false), but if you do true && 1 you get the second (1). Play with this a bit.
- Is there any difference between != and <>?
- Ruby has deprecated <> in favor of !=, so use !=. Other than that there should be no difference.
- Isn't there a shortcut?
- Yes. Any && expression that has a false is immediately false, so you can stop there. Any || expression that has a true is immediately true, so you can stop there. But make sure that you can process the whole expression because later it becomes helpful.