tell-o-SCOPE

Michael Abe
3 min readDec 11, 2020
Photo by Peter Kasprzyk on Unsplash

Scope in the context of coding is just that; keeping things in context. While it may be an oversimplification, scope on a basic level comes down to levels themselves, specifically the level where methods, variables, and constants exist.

The three “levels” of scope that we focus here will be local, instance, and class. To display these three levels and go into further detail, we will be using variables as our example.

local_variable = A variable that exists and can only be called on in the code that it is created in. A local variable declared in a loop only exists in that loop. A local variable declared in a method only exists in that particular method. Local variables are identified by starting with a lowercase letter or underscore.

def tiger
coat_color = "Orange with black stripes"
puts coat_color
end
def lion
coat_color = "Tan"
puts coat_color
end
tiger
lion

In the example above both methods have and output the variable “coat_color”. By running the code above, we would see that our “tiger” method outputs “"Orange with black stripes” while our “lion” method displays “Tan”. How can this be if both of our variables have the same name? Scope! Since both instances of our “coat_color “ variables are local variables, their assignment exists only in their respective methods.

@instance_variable = A variable that can only be accessed within a specific object but available to all methods in it’s class. Instance variables are specified by a single “@” symbol as it’s first character.

@@class_variable = A variable that is common or shared through all instances of the class that it exists in. Class variables can only be used by objects in it’s class. Class variables are denoted by starting with two “@” symbols.

class Cat
attr_accessor :species, :coat_color

@@all = []

def initialize(species, coat_color)
@species = species
@coat_color = coat_color
@@all << self
end
def self.all
@@all
end
end
tiger = Cat.new("Tiger", "orange with black stripes")
lion = Cat.new("Lion", "tan")
puts tiger.coat_color
puts lion.coat_color
puts Cat.all

In the code above we have both instance variables and a class variable. The instance variables are identified by the single “@” symbol. In this particular example, the instance variables “@species” and “@coat_color” are declared upon initialization and are specific to the object that is created. By running this code we would see tiger.coat_color output the string “Orange with black stripes” while lion.coat_color would output the string “tan”. The scope of “coat_color” is the objects themselves.

Noted by the double “@” symbols, the class variable in the previous example is “@@all”. Since the newly created cat class objects are all pushed to the array assigned to our “@@all” class variable in the initialize method, the results of outputting the “@@all” class variable itself, would be the unique object id’s of any objects that we create through the initialize method. In this case there are only two. The id for the “tiger” object and the id for the “lion” object would both be displayed when running the code above.

In conclusion, understanding and being mindful of scope is an important part of creating any program. By knowing where a variable, method, or constant is visible we can ensure that we know what is available to us at different parts of our program. Like sailors at sea, we can use our “scope” to help us visualize and navigate!

--

--