Giter Site home page Giter Site logo

method-scope-lab-v-000's Introduction

Method Scope Lab

Objectives

  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.

Instructions

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
end

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
  end
end

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:

NameError:
  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
  end
end

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."
end

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 following lines in your lib/rescue_princess_peach.rb file:

rescue_princess_peach
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.

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)
  end
end

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.

View Method Scope Lab on Learn.co and start learning to code for free.

method-scope-lab-v-000's People

Contributors

annjohn avatar aviflombaum avatar cjbrock avatar dakotalmartinez avatar dandanberry avatar ddeleon267 avatar deniznida avatar jenjiyi avatar lcorr8 avatar maxwellbenton avatar piyushwaghwani avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

method-scope-lab-v-000's Issues

Spec file is incorrect

according to the spec file, catch_phrase does not accept an argument

expect{catch_phrase}.to output("It's-a me, Mario!\n").to_stdout
end

this causes an undefined error for phrase.

spec file should be:

expect{catch_phrase(phrase)}.to output("It's-a me, Mario!\n").to_stdout
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.