We have reached the end of the “purely linear” programming lessons. What I mean by purely linear is everything we have learned up to now has been programmed and run in a mostly straight line. Sure, we bounce into functions, and we manipulate values, but the scripts themselves have been orderly, one sided affairs where the lines in the script are just followed and each step run. There’s no choice in the matter, each step gets executed.
Real programs don’t work that way, though (well, generally they don’t). Most programs require users to make decisions and choices, or perform based on conditions. To do that, we have to understand how the computer makes choices. In short, we need to learn and understand “logic”.
Most of the logic that we will learn is pretty straightforward. There’s some really deep stuff that we could get into, but suffice it to say that for most of what an everyday Ruby programmer will do, there are just a few things to remember. There’s also lots of ways of implementing those few things.
So our first step is to, yep, memorize some logic, or “truth” tables. Zed recommends doing this exercise for an entire week. I don’t have the luxury of doing that, so I will dedicate some extra time in this day to focus on it. That may be a hindrance later, or I will just have to keep with the drill and see what happens (it’s the reason I keep my “all_code” file, too 🙂 ). So let’s do this…
From Zed and Rob:
Here’s a tip on how to memorize something without going insane: Do a tiny bit at a time throughout the day and mark down what you need to work on most. Do not try to sit down for two hours straight and memorize these tables. This won’t work. Your brain will really only retain whatever you studied in the first 15 or 30 minutes anyway.Instead, what you should do is create a bunch of index cards with each column on the left on one side (True or False) and the column on the right on the back. You should then pull them out, see the “True or False” and be able to immediately say “True! (or False!)” Keep practicing until you can do this.
Once you can do that, start writing out your own truth tables each night into a notebook. Do not just copy them. Try to do them from memory, and when you get stuck glance quickly at the ones I have here to refresh your memory. Doing this will train your brain to remember the whole table.
Do not spend more than one week on this, because you will be applying it as you go.
So here are the terms Ruby uses to determine if a statement of a condition is “TRUE” or “FALSE”. Logic to a computer is basically a series of “gates”, and based on whether or not a statement evaluates to TRUE or FALSE determines where a signal will flow.The terms are:
- != (not equal)
- == (equal)
- >= (greater-than-equal)
- <= (less-than-equal)
You actually have run into these characters before, but maybe not the phrases. The phrases (and, or, not) actually work the way you expect them to, just like in English.
The Truth Table
We will now use these characters to make the truth tables you need to memorize.
This is interesting, and it requires me to take a step back and think about what I’m actually looking at. Some of this is counter-intuitive, but if we stop to think about it for a bit, these statements make sense. The goal, of course is so that we can recognize these true/false values when we want to cause our instructions to go one place in a program, or got to another. Logic and the confirmation of a situation being true or false is the key to getting programs to go from one point to another and to return if we want it to. I’m going to make a guess at this point that decisions and decision trees are going to be in our near term future :).