Ruby - Explain Variable Scopes

1. Introduction

Variable scope refers to the regions of the code from where a variable can be accessed or modified. Ruby, like other programming languages, provides various scopes for variables. In this post, we delve into the different scopes offered in Ruby, including local, global, instance, and class scopes.

Local Variables: 

Definition: Variables that are defined inside a method or a block and are not available outside that method or block. 

Characteristics: They begin with a lowercase letter or an underscore. Their scope is restricted to the block of code or method in which they are defined. 

Instance Variables: 

Definition: Variables that are available across methods for any particular instance (or object) of a class. 

Characteristics: They begin with the @ symbol. They are used to hold the state of an object. 

Class Variables: 

Definition: Variables that belong to the class, rather than any particular object. They are shared among all instances of a class. 

Characteristics: They start with @@ and are consistent across all objects of a given class. 

Global Variables: 

Definition: Variables that are available throughout your entire application, regardless of where they are declared. 

Characteristics: They start with the $ symbol. It's typically advised to avoid them unless there's a specific reason because of their wide-reaching implications.

2. Program Steps

1. Launch your Ruby development environment.

2. Create a program that defines variables of different scopes: local, global, instance, and class.

3. Access these variables within various parts of the code to demonstrate their accessibility and limitations.

3. Code Program

# Local variable definition
local_var = "I am a local variable"

# Global variable definition
$global_var = "I am a global variable"

# Defining a class to demonstrate instance and class variable scopes
class ScopeDemo
  # Class variable definition
  @@class_var = "I am a class variable"
  # Constructor to define an instance variable
  def initialize
    @instance_var = "I am an instance variable"
  end
  # A method to showcase the accessibility of different variable scopes
  def show_variables
    begin
      puts local_var  # This will throw an error
    rescue
      puts "Error: Local variable is not accessible inside the class method!"
    end
    puts $global_var
    puts @@class_var
    puts @instance_var
  end
end
# Instantiate the class and invoke the method
scope_obj = ScopeDemo.new
scope_obj.show_variables

Output:

Error: Local variable is not accessible inside the class method!
I am a global variable
I am a class variable
I am an instance variable

Explanation:

1. local_var: Local variables are limited in scope to the block, method, or module where they are defined. Hence, attempting to access them outside of their defined scope, as in our class method, results in an error.

2. $global_var: A global variable is defined with a $ prefix and is accessible from any part of the code, irrespective of where it was defined.

3. @@class_var: Class variables, signified with @@, are shared among all instances of a class and the class itself. They are accessible from both instance and class methods.

4. @instance_var: An instance variable, marked with @, is tied to a particular object (or instance) of the class. Every instance of the class can have different values for its instance variables.


Comments