
Ruby provides five types of variables:
Local Variables: variables defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin witha lowercase letteror_.Instance Variables: variables available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign@followed by the variable name.Class Variables: variables available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign@@and are followed by the variable name.Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign$.
1. Local Variables:
Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block’s opening brace to its close brace {}.
When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments.
Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program.
2. Instance Variables
Instance variables begin with @. Uninitialized instance variables have the value nil
Here is an example showing usage of Instance Variables.
1 |
class |
Output:
1 |
Customer id 1 |
3. Class Variables
Class variables begin with @@ and must be initialized before they can be used in method definitions.
Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.
Here is an example showing usage of class variable:
1 |
class |
Output:
1 |
Total number of customers: 1 |
4. Global Variables
Global variables begin with $. Uninitialized global variables have the value nil
Assignment to global variables alters global status. It is not recommended to use global variables.
1 |
$global_variable = 10 |
Output:
1 |
Global variable in Class1 is 10 |
Ruby Constants
Constants begin with an uppercase letter.
Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.
Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.
1 |
class Example |
Output:
1 |
Value of first Constant is 100 |
Ruby Pseudo-Variables
They are special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables.
self: The receiver object of the current method.true: Value representing true.false: Value representing false.nil: Value representing undefined.__FILE__: The name of the current source file.__LINE__: The current line number in the source file.




近期评论