Variables in Ruby
ruby February 27th, 2008Declaring 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.
@variableAn example of a class variable:
@@variable
March 1st, 2008 at 4:44 pm
Hmm… that’s very interesting. Thanks!
May 16th, 2008 at 8:46 pm
Hello my friends
