Declaring variables in Ruby is easy. Let’s start by giving the string and integer example.

# variable example
variable = 'string'; #string
number = 10; #integer

Wasn’t that easy. Try running this program:

#variable in output example
name = "adriaan";
print "My name is " + name + ".\n";

There are more sorts of variables in Ruby. There are constants, global variables, class variables and instance variables.

Constants are always uppercase.

PI = 3.141592654;
print PI;
 
#output 3.141592654

If you put $ before the variable. You declare the variable a global variable.

$global = "global variable";

Instance variables are created by putting @ in front of the variable. These variables are an instance of the variable ’self’. They belong to the object itself.

@variable

An example of a class variable:

@@variable