Variables in Ruby

Juzer Shakir
3 min readJun 30, 2021

--

An essential object to which very few programs exist without it if not at all.

Variable is a placeholder that represent values such as strings, integer, float, complex numbers, boolean values, arrays, hashes, lambda & non-lambda (Proc). These values can later be accessed and modified when and where necessary in a program.

Types of Variables

There are 4 types of variables, and the difference between each of these is how their scope is defined in a program.

  1. Local Variable
  2. Instance Variable
  3. Class Variable
  4. Global Variable

Local Variable

Local variables are variables whose scope/access is limited to where they’re defined.

If they’re defined in a root program, they cannot be accessed inside other methods/classes/modules.

If they’re defined in a method/class/module, they cannot be accessed in nested methods/classes/modules or outside to where it's defined.

To define this variable:

  1. The initial character will be a lowercase letter or an underscore ‘_’. No digits.
  2. No special characters are allowed. (to any variable type)
  3. Digits are allowed within the variable name. (to any variable type)

Instance Variable

Instance variables are variables that are available to all methods inside a class for any particular object or class instance. Their value might change from object to object.

Their values are only available from one object to another if it's explicitly defined.

These variables are not available to nested classes or modules or outside of where it's defined.

To define this variable, begin with @ followed by the variable name.

We initiate these variables inside the initialize method in a class so that it's accessible throughout the scope of that class.

We cannot access instance variables’ value as a class method but we can access it as an instance method to the object of a class with the help of Accessor methods in Ruby.

Tip: If you want the list of all instance variables in a class, you can get by giving .instance_variables method to the object only after initializing the class but not to the class itself.

Class Variable

Class variables are variables that are available to all methods inside a class for all objects or class instances. Their values don’t change from object to object.

Their values are available to all objects initiated by the same class.

These variables are available to all nested classes for where it's defined.

These variables are not available to other class/classes.

To define this variable, begin with @@ followed by the variable name.

We can initiate these variables wherever we want in a class but it’s best practice to initiate these at the start of a class.

We can neither access these variables as an instance method to the object of a class nor class itself.

Tip: If you want list of all class variables in a class, you can get by giving .class_variables method to the class itself and not object.

Global Variable

Global variables are variables that are available globally (throughout) in the program.

These are available throughout all methods/classes/modules in a program.

To define this variable, begin with $ followed by the variable name.

We can initiate these variables wherever we want in a program but it’s best practice to initiate these at the start of a program.

There it is! A little insight on what they’re, what their scope is throughout the program, how we define it, and where to define it.

My next article: Constants in Ruby

--

--