Variables Scope in Ruby

Written by Supakorn Laohasongkram on August 14th, 2014

There are six types of Ruby variables: global, local, class, instance, contant, and psuedo variables. Each type differs by its scope and behavior. To make the distinction clearer, we will go through each, one by one. Here is a summary...


Global Variable

Global variable works across all your code. You can identified the variable by dollar sign($) in front. (You can see the example below.) However, global variable should be used with caution since it would run through the whole code and you could easily forgot or unknowinly overwrite it. And this in turns, could effect your whole code.

$global_variable = 10
class Class1 # call inside of class 1
  def print_global 
     puts "Global variable in Class1 is #$global_variable" 
  end
end 
class Class2 # call inside of class 2
  def print_global
     puts "Global variable in Class2 is #$global_variable"
  end
end	

global_variable # or call outside of class which output => 10
class1obj = Class1.new
class1obj.print_global # output => Global variable in Class2 is 10
class2obj = Class2.new
class2obj.print_global # output => Global variable in Class1 is 10
							

As you can see, the global variable of global_variable could call in and outside of class anywhere.

Local Variable

Local variable works depending on the corresponding start and end of a statement. This could be identified by the "{}" sign or the designated syntax like "do" and "end" which defines the beginning and the end of a method. To defined a local variable simply write in lower case or begins with a underscore(_).

local_variable = 10 # first way of defining local variable
_local_variable = 10 # second way of defining local variable

class Class1 
  def print_local 
  	 local_variable = 10
     puts "local variable in Class1 is #{local_variable}" 
  end
end 
class Class2
  def print_local
     puts "local variable in Class2 is #{local_variable}"
  end
end	
p local_variable # output => 10

class1obj = Class1.new
class1obj.print_local # output => local variable in Class1 is 10
class2obj = Class2.new
class2obj.print_local # output => "undefined local variable or method `local_variable'"
							

Class Variable

Class variable is identified by the "@@" sign in front. The scope of this variable is inheritable all other instances of this class.

class Class1 
  def print_class 
  	 @@class_variable = 10
     puts "Class variable in Class1 is #{@@class_variable}" 
  end
end 

@@class_variable # output => Can't access. "warning: class variable access from toplevel" 

class1obj = Class1.new # defining class instance
class1obj.print_class # output => class variable in Class1 is 10

class1obj2 = Class1.new # defining another class instance
class1obj2.print_class # output => class variable in Class1 is 10

class1obj3 = Class1.new # defining another class instance
class1obj3.print_class # output => class variable in Class1 is 10
							

From the example above, the class variable of @@class_variable is passed on through all of the class instances of Class1.

Instance Variable

Instance Variable is identified by the "@" sign in front, the scope of this varible is only inside the class it is decleared upon.

class Class1 
	attr_reader :instance_variable
	def initialize(instance_variable)
	  @instance_variable = instance_variable
	end
  def print_instance 
     puts "Instance variable in Class1 is #{@instance_variable}" 
  end
  def print
  	 puts "Instance variable in Class1 is #{@instance_variable}" 
  end
  def print2
  	 puts "Instance variable in Class1 is #{@instance_variable}"   	
  end
end 

class1obj = Class1.new(10) # defining new class instance with 10 as argument
class1obj.print_instance # output => Instance variable in Class1 is 10
class1obj.print # output => Instance variable in Class1 is 10
class1obj.print2 # output => Instance variable in Class1 is 10
instance_variable # output => "undefined local variable or method `instance_variable' for main:Object"
							

You can see that the @instance_variable is shared among three methods inside Class1. And when the variable is call outside of Class1 it gives an error.

Constant Variable

Constant variable is identified by the all capitalized letters like so, "VAR = 10." If it is defined within a class then scope is only within that class. However, if the constant is defined outside of a class its scope becomes global.

BYE = "BYE"

class Class1 
	HELLO = "HELLO"
	def greet
	  p HELLO
	end
	def bye
	  p BYE
	end
end 

class1obj = Class1.new # defining new class instance
class1obj.greet # output => "HELLO"
class1obj.bye # output => "BYE"
HELLO # output => Get an error saying "uninitialized constant HELLO"
							

© Copyright Supakorn Laohasongkram 2014