|
If you are new to programming all together, the term "variable" may be completely new to you.
This is why, before discussing variables in Ruby, I believe it would be much more appropriate to define what a variable is first.
Let's cite Wikipedia:
"A variable is a symbolic representation denoting a quantity or expression" Hum...sounds too complicated. Let me give you an example:
x=5
This piece of code give "x" a value of 5. Now, let's say we have this:
y=3
x=5
if (x==5)
x=x+y
end
The code above tests if x equals to 5 first, and if it is, then x takes a new value: the former value of x, which is 5 and adds y's value to it.In the end, x will be equal to 8.
Variables are absolutely necessary in writing computer code. Most of the data would not be able to be modified without using variables.
Different types of variables exist in different programming languages, some may or may not be implemented in others. For the purpose of this tutorial, we will continue to focus only on variables in Ruby programming language.
With variables you can control data objects referenced.
In Ruby you can have:
number = 5
or
name = "mark"
In the first case, Ruby will "know" that number actually means 5. Think of it as a sort of nickname.
In the 2nd example, Ruby will know that name is actually called "mark"
There are 5 types of variables in Ruby: constants,locals, global, instance and class.
Let's talk about what each of them does:
1) a constant is of course...constant
2) locals can only be accessed from a part of the program
3) globals are accessible globally to the program
4) instance variables are associated to an object
5) class variables belong to a class
Do not worry if you don't know all these terms, you will the more you read from the tutorial.
This is pretty much as far as we're going to go into the topic of variables for the moment. You should already know the basics of variables by now after reading the above.
To differentiate between all the types above, Ruby uses special characters.
1) a constant begins with an uppercase letter and it should not be defined inside a method
2) a local must begin with a lowercase letter or the _ underscore sign
3) a global begins with the $ sign; an uninitialized global has the value of "nil" and also produces a warning
4) instances begin with the @ sign; an uninitialized instance has the value of "nil" and also produces a warning
5) a class variable begins with double @@ and have to be first initialized before being used in a method definition, otherwise you will get an error if you refer to it without initializing
Initializing a variable means giving it an initial value that will also determine the type of the variable. It can be something insignificant, such as 0 to simply serve the purpose of initialization. Variables can be modified later in the program.
Besides true variables, pseudo-variables also exist (true, false, nil, self, __FILE__ and __LINE__ denoting the current file, respectively the current line in the file). True and false are pretty self-explanatory, self should be also, and nil has most of the times the meaning of "null" or "empty" or "not defined".
They are called pseudo-variables because they don't behave like real variables would, instead they act more like constants.
These are reserved words in Ruby. You must not use them as names of variables or functions or anything else beyond the purpose for which they have been created.
alias and BEGIN begin break case class def defined
do else elsif END end ensure false for if
in module next nil not or redo rescue retry
return self super then true undef unless until when
while yield
As Ruby continues to improve as a programming language, other reserved words might be added in future releases.
|