In the last post on Ruby, where we worked with User Input, we used a few Variables. Variables are ultra-useful and ultra-important. It’s great to do some stuff with Ruby, but you’ll very quickly want to do some stuff that involves setting, getting, storing and retrieving data. I’m struggling to think of a really good example of doing something useful, that doesn’t involve data being stored at least temporarily.
Fortunately, we have Variables. These allow us to store data and then give that data a name that we can refer to later, when we want to use the data in some way. Imagine your kitchen with its Coffee, Tea and Sugar containers, fill them with the right stuff and store them for use later. When the wife asks for a cup of tea, bingo she’s used the reference name so you’ll know where to get the right stuff from. OK, not a perfect analogy but you get the idea!
A variable might look like these:
string = “David” or maybe integer = 12345 or maybe float = 12345.123
Just in these examples there’s quite a lot shown. First, we’ve declared a variable by designating it the name string and assigned a value which is a string. We do assignment here using the = operator. In the second example we did the above but assigned a value which is an integer, then another for float. Remembering that an integer doesn’t have a fractional or decimal component (i.e. it’s not like 12345.67). When a number in Ruby has ‘decimal points’, it’s usually called a Floating Point number or Float for short.
As computers are all about math, there’s a large set of Operators, which all have a Precedence, just like in real world math. However, we’ll stick with the basics here and follow up with that in a later post. For now, we understand how to declare a variable of varying Type and assign it a value. Actually, let’s quickly cover Type while were here. Fortunately Ruby makes it easy.
Some languages like Java, require you to declare a variable and define the variable type at the same time. Not Ruby. Unlike Java and others Ruby doesn’t use strong / static variable typing. Ruby follows the convention of Dynamic Typing or Duck Typing as it is often referred to. When you assign a value to a variable, Ruby will Type it dynamically and it isn’t set in stone either. Change the value and Ruby will dynamically change the Type again. This is a long winded way of saying, just define your variables and move on! However, for the sake of study, let’s look at some examples.
· Open irb (Start > CMD > irb) and type in, string = “David”
· Now type: string.kind_of? String
· Now type: string.class
Pretty cool! Try it with the others and see what you get. Here we used to Methods of the Object (1) class, namely kind_of? and class. You can imagine your programme using data from another, but before doing so you want to check it’s the right type. Which reminds me, just assigning a variable a different type is a bit brute-force and obviously permanently changes the variable type. What if you want to keep its type (because say another system needs a particular type), but when you use it convert it only sometimes? Don’t worry there’s methods for that and generally a good idea to use.
The methods are to_f and to_s, I’m guessing you can work out which is which. Try this:
· In irb type: myString = “012345”
· Then confirm the type: myString.class
· Now type: MyString.to_f
· Then just to check type: MyString.class
You can see that using this method of the Fixnum (2) class is much less destructive. Now, the inquisitive will be wondering where to_i is to convert something, say a float, to an integer. Oddly, it’s hiding in the String (3) class we encountered previously. This is to do with Class Hierarchy and which is the Parent Class of a given class. If you look on the String class Ruby Doc page, you’ll see the parent is Object, tada!
I’ll keep saying, it’s good to read the Docs. For example, in String we see it has to_f listed with other examples of how to use it. One is “45.67 degrees”.to_f, try it in irb. That sort of thing could come in very handy when working with data.
Nothing to Declare
All pretty cool stuff, except for the slight mistruth I snuck in at the start… woops. In programming, Variables are declared in order to specify their Type, how they can be modified and what their ‘scope’ is (we’ll cover that next). Of course, as a careful reading of the above will reveal, Ruby doesn’t care about any of that. Therefore, you don’t really declare variables in Ruby. However, it conversation you’re likely to say that so others understand, it’s easier than explaining “I’m now not declaring my variables”.
NEXT Post: Categories and Scope of Variables (Global, Local, Instance, Constant)…
Links for the studious