Method Scope Lab


  1. Define a method that takes in an argument and pass a variable in as that argument.
  2. Understand that a variable defined inside a method cannot be used outside of that method.


Part I: Passing a Variable Into a Method

Open up lib/catch_phrase.rb. You should see the following method:

def catch_phrase
  puts phrase

Note that the method is trying to puts out a variable called phrase.

Let's take a look at the test for this method in spec/catch_phrase_spec.rb:

describe "#catch_phrase" do
  it "puts out a catch phrase" do
    phrase = "It's-a me, Mario!"
    expect{catch_phrase}.to output("It's-a me, Mario!\n").to_stdout

Go ahead and run the test for this method only by typing rspec spec/catch_phrase_spec.rb into your terminal in the directory of this lab. You should see the following error:

  undefined local variable or method `phrase' for #<RSpec::ExampleGroups::CatchPhrase:0x007f87b9cf04c0>

This error is occurring because the code inside the #catch_phrase method is trying to use the phrase variable but it's not present inside the scope of the #catch_phrase method. It is out of scope. Let's fix it!

We need to pass phrase into our #catch_phrase as an argument. Let's do it:

  1. Re-define the #catch_phrase method to take in an argument of a phrase.
  2. Change the test in spec/catch_phrase_spec.rb to match the following:
require "spec_helper"
describe "#catch_phrase" do
  it "puts out a catch phrase" do
    phrase = "It's-a me, Mario!"
    expect{catch_phrase(phrase)}.to output("It's-a me, Mario!\n").to_stdout

Part II: Understanding Method Scope

Open up lib/rescue_princess_peach.rb and take a look at the following method:

def rescue_princess_peach
  status = "rescued"
  puts "Hooray! Mario has rescued Princess Peach."

Notice that the body of this method is setting a variable, status equal to a value of "rescued". Do you think we will be able to access this variable outside of the method? Let's find out!

1 . Un-comment the lines below in your lib/rescue_princess_peach.rb file. You can do this by removing the # at the front of each line.

puts status

2 . Run the file with ruby lib/rescue_princess_peach.rb in your terminal. You should see the following:

Hooray! Mario has rescued Princess Peach.
lib/rescue_princess_peach.rb:9:in `<main>': undefined local variable or method `status' for main:Object (NameError)

We are getting a NameError because status is undefined. Wait a minute, you might be wondering. Didn't we define status inside the #rescue_princess_peach method? We did, but variables defined inside a method are not available outside of that method. They are only available within the scope of that method.

Go back and comment out lines 8 and 9 of rescue_princess_peach.rb by adding back the #s.

Run the test suite and you'll see that we are passing all of our tests. If you open up the spec/rescue_princess_peach_spec.rb file, you'll see the following test:

require "spec_helper"

describe "#rescue_princess_peach" do
  it "outputs a message and sets a variable, status, that is not available outside of this method" do
    expect{rescue_princess_peach}.to output("Hooray! Mario has rescued Princess Peach.\n").to_stdout
    expect{puts status}.to raise_error(NameError)

Notice the last expectation of our test: expect{puts status}.to raise_error(NameError). We expect any attempt to use the status variable to be met with a NameError. Our program, outside of the #rescue_princess_peachmethod, just doesn't know what it is.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us