Understanding Ruby Variables

Revision as of 21:13, 30 November 2007 by Neil (Talk | contribs) (Converting Variable Values)

Revision as of 21:13, 30 November 2007 by Neil (Talk | contribs) (Converting Variable Values)

PreviousTable of ContentsNext
Commenting Ruby CodeRuby Variable Scope


Variables are essentially a way to store a value and assign a name to that value for reference purposes. Variables take various forms ranging from integers to strings of characters. In this chapter we will take a look at how variables are declared and converted. We will also look at the much simpler area of Ruby constants.

Ruby Constants

A Ruby constant is used to store a value for the duration of a Ruby program's execution. Constants are declared by beginning the variable name with a capital letter (a common convention for declaring constants is to use uppercase letters for the entire name). For example:

MYCONSTANT = "hello"
=> "hello"

Unlike other programming languages, Ruby actually allows the value assigned to a constant to be changed after it has been created. The Ruby interpreter will, however, issue a warning - even though it allows the change:

MYCONSTANT = "hello2"
(irb):34: warning: already initialized constant Myconstant
=> "hello2"

Ruby and Variable Dynamic Typing

Many languages such as Java and C use what is known as strong or static variable typing. This means that when you declare a variable in your application code you must define the variable type. For example if the variable is required to store an integer value, you must declare the variable as an integer type. With such languages, when a varaible has been declared as a particular type, the type cannot be changed.

Ruby, on the other hand, is a dynamically typed language. This has a couple of key advantages. Firstly it means that you do not need to declare a type when creating a variable. Instead, the Ruby interpreter looks at the type of value you are assigning to the variable and dynamically works out the variable type. Another advantage of this is that once a variable has been declared, you can dynamically change the variable type later in your code.


Declaring a Variable

Variables are declared and assigned values by placing the variable name and the value either side of the assignment operator (=). For example, to assign a value of 10 to a variable we will designate as "y" we would write the following:

y = 10

We have now created a variable called y and assigned it the value of 10.

In common with some other scripting languages, Ruby supports something called parallel assignment. This is useful if you need to assign values to a number of variables. One way to do this would be as follows:

a = 10
b = 20
c = 30
d = 40

The same result can be achieved more quickly, however, using parallel assignment:

a, b, c, d = 10, 20, 30, 40

Identifying a Ruby Variable Type

Once a Ruby variable has been declared it can often be helpful to find out the variable type. This can be achieved using the kind_of? method of the Object class. For example, to find out if our variable is an Integer we can use the kind_of? method:

y.kind_of? Integer
=> true

We can also ask the variable exactly what class of variable it is using the class method:

y.class
=> Fixnum

This tells us that the variable is a fixed number class.

Similarly, we could perform the same task on a string variable we will call s:

s = "hello"
s.class
=> String

Here we see that the variable is of type String.

Changing Variable Type

One of simplest ways to change the type of a variable is to simply assign a new value to it. Ruby will dynamically change the type for that variable to match the type of the new value assigned. For example, we can create a variable containing an integer and verify the type:

x = 10
=> 10
x.class
=> Fixnum

Suppose, we now want to store a string in a variable named 'x'. All we need to do is make the assignment, and Ruby will change the variable type for us:

x = "hello"
=> "hello"
x.class
=> String

As we can see, the x variable is now a string.

Converting Variable Values

It is important to note that the above approach is a somewhat destructive way to change a variable type. Often something a little more subtle is needed. For example, we might want to read the value from a variable but convert the extracted value to a different type. The Ruby variable classes have methods that can be called to convert their value to a different type. For example, the Fixnum class has a method named to_f that can be used to retrieve the integer stored in a variable as a floating point value:

y = 20
=> 20
y.to_f
=> 20.0

Similarly, you can convert a Ruby integer to a string using the to_s() method. The to_s() method takes as an argument the number base to which you want the conversion made. If no number base is specified, decimal is assumed:

54321.to_s
=> "54321"

Alternatively, we could convert to binary by specifying a number base of 2:

54321.to_s(2)
=> "1101010000110001"

Or even to hexadecimal or octal:

54321.to_s(16)
=> "d431"
54321.to_s(8)
=> "152061"

In fact, we can use any number base between 1 and 36 when using the to_s method.



PreviousTable of ContentsNext
Commenting Ruby CodeRuby Variable Scope