Scope of Local Variables

In this chapter, you will learn about the visibility of local variables.

Target

Scope

The focus is on using language constructs such as def, class and module and how it affects the visibility.

At the Top Level

Visibility of Local Variable

Let's look at an example where we have a local variable at the top level and see if we can access it inside a top level method.

x = 1

p x

def test
  p x
end

test

This prints 1 and then the error.

NameError: undefined local variable or method ‘x’ for main:Object

Thus, we can see that the top level local variable x is not accessible inside the top level method.

The x at the Top Level and Inside Method

We can verify this fact by asking Ruby.

x = 1

p "Local variables at the top level : #{local_variables}"

def test
  p "Local variables inside the test method : #{local_variables}"
end

test

This prints:

Local variables at the top level : [:x]
Local variables inside the test method : []

This shows that at the top level, there is a local variable x, whereas, inside the top level method, there is none. This is an example where self does not change but scope changes.

Any variables defined within a method goes out of scope when we exit the method. The local variable is garbage collected. For instance, any variable defined in a learn method is not available outside of that method.

Scope of Variables in Top Level Method

The diagram illustrates the life time of a variable defined within a method.

Inside a Top Level Method

Local Variables in Different Scopes

What happens if we define a local variable inside the top level method? Let's write a simple program that defines local variable at the top level and inside a top level method.

x = 1

p "At top level x is : #{x}"

def test
  x = 2
  p "Inside the top level method x is : #{x}"
end

test
p "Back at the top level x is : #{x}"

This prints:

At top level x is : 1
Inside the top level method x is : 2
Back at the top level x is : 1

Top level has its own scope. The method definition has its own scope.

The scope at Top Level and Inside Method

That's why we see x is 1 at top level and it is 2 inside the method definition. The variable names are the same. However, the x at the top level and the x inside the method are variables in different scopes. Thus, they are different.

Different Local Variables with Same Name

This is an example where self does not change, but scope changes. We know that the self is main at the top level as well as inside the method.

The Top Level Scratch Pad

The Top Level Method Scratch Pad

Let's trace the local variables in different scopes as the program executes.

x = 1

p "At the top level local variables:"
p local_variables

def test
  x = 2
  p "Inside the top level method, local variables:"
  p local_variables
end

test

This prints:

At the top level local variables:
[:x]
Inside the top level method, local variables:
[:x]

The name of the local variables is the same. From the previous experiment we know they are different.

Inside a Class

Visibility of Local Variable

What happens when we try to access a local variable defined at the top level from a class?

x = 1

class A
  p x
end

This gives us the error:

NameError: undefined local variable or method ‘x’ for A:Class

The local variable x defined at the top level is not visible within the class.

The Visibility of Local Variable Top Level and Class

This is an example where both self and scope changes.

Local Variables in Different Scopes

Let's write a simple program that defines a local variable at the top level and inside a class.

x = 1

p "At the top level, x is #{x}"

class A
  x = 2
  p "Inside the class, x is : #{x}"
end

p "Back at the top level, x is #{x}"

This prints:

At the top level, x is 1
Inside the class, x is : 2
Back at the top level, x is 1

The value of x inside the class is different from the x at the top level. Variables inside the class have its own scope. The x at the top level is different from the x inside the class.

Variables with Same Name in Different Scope

This is an example where both self and scope changes. The self at the top level is main and the self inside the class is A.

The Self at Top Level and Inside Class

Let's trace the local variables.

x = 1

p "Local variables at the top level : #{local_variables}"

class A
  x = 2
  p "Local variables inside the class : #{local_variables}"
end

This prints:

Local variables at the top level : [:x]
Local variables inside the class : [:x]

The name of the local variable is the same. But they are different because they belong to different scopes. The first one belongs to the top level and the second one belongs to the method level scope.

The Local Variable at Top Level Scope and Class Level Scope

Let's take a look an example.

x = 1

p "Local variables at the top level : #{x}"

class A
  x = 2
  p "Local variables inside the class : #{x}"
end

p "Local variables at the top level : #{x}"

This prints:

Local variables at the top level : 1
Local variables inside the class : 2
Local variables at the top level : 1

Inside a Module

Visibility of Local Variable

What happens when we try to access a local variable defined at the top level from a module?

x = 1
module B
  p x
end

This gives us the error:

NameError: undefined local variable or method ‘x’ for B:Module

The local variable x defined at the top level is not visible within the module.

The Variable at Top Level and Inside Module

This is an example where both self and scope changes.

Local Variables in Different Scopes

Let's write a simple program that defines local variable at the top level and inside a module.

x = 1

p "At the top level, x is : #{x}"

module B
  x = 2
  p "Inside the module, x is : #{x}"
end

p "Back at the top level, x is : #{x}"

This prints:

At the top level, x is : 1
Inside the module, x is : 2
Back at the top level, x is : 1

The variables inside Module also have its own scope.

Different Local Variables with Same Name

The self and scope both change in this example. The self is main at the top level and the self is B inside the module.

The Self at Top Level and Inside Module

Discovery Exercise

Write a program to trace the local variables for the above example.

The Grand Example

Let's combine top level, class definition and method definition into one example.

x = 1

p "At the top level, x is : #{x}"

def test
  x = 2
  p "Inside top level method, x is : #{x}"
end

test 

class A
  x = 3
  p "Inside the class, x is : #{x}"
end

module B
  x = 4
  p "Inside the module, x is : #{x}"
end

p "Back at the top level, x is : #{x}"

This prints:

At the top level, x is : 1
Inside top level method, x is : 2
Inside the class, x is : 3
Inside the module, x is : 4
Back at the top level, x is : 1

We see that top level, top level method, class and module have their own scopes and the value of x is specific to their own scope.

Discovery Exercise

In the last example, does the self and scope change together? If so, what are their values as the program executes?

Key Takeaways

Here is a summary of how self and scope changes.

Self Scope Where
No Change Changes Top Level and Top Level Method
Changes Changes Top Level and Inside Class
Changes Changes Top Level and Inside Module

Summary

In this chapter, we experimented with the scope of variables in four different scenarios.

  1. At top level scope
  2. Method definition scope
  3. Class definition scope
  4. Module definition scope

We found that each of these, have their own local variables. The class, module or def keyword creates a new local scope.

results matching ""

    No results matching ""