Understanding scope in Ruby

Lewis Coldwell
The Yorkshire Coder
3 min readMar 6, 2018

Getting to grips with scope is imperative to the basic operation of Ruby code. When you’re presented with “scope” try and think of two simple things: visibility and variables. We want our methods and classes to be able to see the variables we want them to use. This is essentially what scope is all about. In my previous post we went through how to construct a (albeit quite basic) banking app. Its functionality depended on calling variables into methods so they could read them.

Let’s put an end to this.

Lets start with an example first:

def sayname
puts "Your name is #{name}"
end
def whatname
puts "Please enter your name and I'll repeat back to you"
name = gets.chomp
sayname
end
whatname

In the example above we are wanting to run two methods, one asks the user for the name and generates an input, while the other calls the data received in this method. However, is it within scope? Short answer — no. Why not? Because we haven’t sent the new local variable “name” to the new method. When the method “sayname” attempts to read out ‘name’ it doesn’t know how to define this. We should have sent it after the user typed their name in. Lets do that example again, properly:

def sayname(name)
puts "Your name is #{name}"
end
def whatname
puts "Please enter your name and I'll repeat back to you"
name = gets.chomp
sayname(name)
end
whatname

This is a rather basis example of scope — but most errors lie in these small intricacies of detail which, when understood properly, can relieve you from hours of endless debugging.

As a general rule local variables (like the one above) will always be out of scope if not sent and called into methods or classes they are being used in. This is by far one of the most common errors I have encountered as a result:

NameError (undefined local variable or method)

Understanding how your code enters into a new scope can help to avoid thee easy to solve errors. Think of it this way: every-time you define a class, method or module your code enters a new scope. One way to break down this barrier, or scope gate is to utilise method calls instead. Take a look at this first example:

p1 = 1class SomeClass   p2 = 2
p local_variables #Prints the local variables in scope
def some_method
p3 = 3
p local_variables
end #end of method scope
end #end of class scope
SomeClass.new
someclass.some_method

Looking at the above example we can easily point out what data will be visible when our code is executed. p2 and p3 will be visible but as p1 is located outside the scope gate of the class method, we will not see its contents in scope. If we wanted to bring that variable into scope — it would have to be called in or sent in from outside the class.

We have already seen in the previous post about how we call variables into methods, but working with classes presents a different take on this. In the next few posts I’ll be covering classes in greater detail including RSpec testing.

Examples have been sourced from: https://www.sitepoint.com/understanding-scope-in-ruby/

--

--

Lewis Coldwell
The Yorkshire Coder

Currently studying Full Stack Development at the Coder Academy, Sydney in their bootcamp program.