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 an essential fundamental parts 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.

  1. true && true
  2. false && true
  3. 1 == 1 && 2 == 1
  4. "test" == "test"
  5. 1 == 1 || 2 != 1
  6. true && 1 == 1
  7. false && 0 != 0
  8. true || 1 == 1
  9. "test" == "testing"
  10. 1 != 0 && 2 == 1
  11. "test" != "testing"
  12. "test" == 1
  13. !(true && false)
  14. !(1 == 1 && 0 != 1)
  15. !(10 == 1 || 1000 == 1000)
  16. !(1 != 10 || 3 == 4)
  17. !("testing" == "testing" && "Zed" == "Cool Guy")
  18. 1 == 1 && (!("testing" == 1 || 1 == 0))
  19. "chunky" == "bacon" && (!(3 == 4 || 3 == 3))
  20. 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:

  1. Find an equality test (== or !=) and replace it with its truth.
  2. Find each &&/|| inside parentheses and solve those first.
  3. Find each ! and invert it.
  4. Find any remaining &&/|| and solve it.
  5. 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:

  1. 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)

  2. Find each &&/|| in parentheses ():

    (true || true) is true: true && !(true)

  3. Find each ! and invert it:

    !(true) is false: true && false

  4. 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 give 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

  1. 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 <=.
  2. Write out the names of each of these equality operators. For example, I call != "not equal."
  3. Play with the 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.
  4. Throw away the piece of paper from 3 away 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 your 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.

Video