Mind Your Variables

When writing a program, I always try to have a good feel for what each variable is pointing to: a number, a string, or whatever. Like in the favorite number program, at some point you’ll have the person’s favorite number as a string, and at another point you’ll have it as an integer. It’s important to keep track of which is which, and you can do this by keeping them in different variables.

And name the variables so it’s easy to tell what they are at a glance. If I had a variable for someone’s name, I might call it name, and I would just assume it was a string. If I had someone’s age in a variable, I might call it age, and I’d assume it was an integer. So if I needed to have the string version of someone’s age, I’d try to make that obvious by calling it something like age_string or age_as_string.

I’m not sure you know, but this book started out as an online tutorial. (It was much shorter back then.) I’ve gotten hundreds of emails from people getting stuck. In most of those cases, the problem was a conversion problem. And usually, it was just someone trying to add an integer and a string together. Let’s look at that error a bit more closely:

my_birth_month = ​'August'
my_birth_day = 3
puts my_birth_month + my_birth_day
example.rb:4:in `+': no implicit conversion of Fixnum into String (TypeError)
from example.rb:4:in `<main>'

What is this error telling us? First, what’s a Fixnum? Basically, it’s an integer. For performance reasons, given the way computers are built and such, there are two different classes of integers in Ruby: Fixnums and Bignums. Basically, really big integers are Bignums, and smaller ones are Fixnums. You don’t really need to know this, though; all you need to know is that when you see Fixnum or Bignum, you know it’s an integer.

So, it can’t convert an integer into a string. Well, you know it can convert an integer into a string, but it doesn’t want to without your explicit instructions. (Eh…it’s only a computer, after all, and computers aren’t exactly known for their independent thinking and initiative.) Honestly, it’s probably a good thing, because maybe you don’t want to convert the integer into a string, you know? Maybe you want to convert the string into an integer. It’s the whole “2 plus 5 adding up to 7 or 25” problem we covered here.

It’s easy to get frustrated when your program has errors. I try not to think of them as errors, though. I try to think of them as the pathetic attempts of a socially inept non-native English speaker (your computer) asking for help. If only your computer were a bit more cultured, it might say something more like, “Excuse me, but I’m unclear as to just one small point…did you want me to convert the integer to a string here, or vice versa? Although it’s probably obvious to any human what you are trying to do, I’m just not that bright.” Then it would laugh nervously. Someday our computers will do just that, but in the meantime, pity the poor fool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.146.173