Git Product home page Git Product logo

reading-errors-and-debugging-using-pry-lab-sea01-seng-ft-071320's Introduction

Debugging with Pry

Learning Goals

  • Read and interpret tests
  • Resolve errors with the aid of Pry

Introduction

If you open this lab and run the tests, you'll notice something: everything is broken. Like, broken bad. And guess what? It's on you to fix it!

Keep in mind, this lab is about more than just finding and fixing the errors - it's about understanding how to look for what those errors are. The instructions in this lab are intentionally minimal. Use the test results to get started. Take time to read and interpret the code in each file in the fix_using_tests folder.

  • What do the methods in each file do, or at least, what should they be doing?
  • What is stopping them from successfully doing these tasks?

Add binding.pry into the existing methods to jump into your code and confirm your suspicions about what is going on. Remember to require 'pry' at top of each file you want to pry into.

Hint: Use Multiple binding.pry Lines

Once you've required pry, you can place as many binding.pry lines as you'd like. Use this to your advantage. For example, in the first file you need to fix, fix_using_tests/false_equivalency, the selection method has a conditional statement:

def selection(num)
  if num = 1
     "YUM YUM MUNCH MUNCH MUNCH"
  elsif num = 2
     "HAM HAM HAM IN MY TUMMY"
  end
end

Test your own assumptions about this code by placing two binding.pry lines, one for each potential condition:

def selection(num)
  if num = 1
    binding.pry
     "YUM YUM MUNCH MUNCH MUNCH"
  elsif num = 2
    binding.pry
     "HAM HAM HAM IN MY TUMMY"
  end
end

There are two tests that run for this file. The first passes in 1 as the value for this method. The second passes 2. Your initial assumption may be that we should hit the first binding.pry on the first test. Run learn, and you should see that this is correct. Type exit while in Pry to continue on to the second test. You might assume that you'll hit the second binding.pry now, but you don't! Why is that? Read the provided code carefully.

My binding.pry Isn't Stopping the Code

Sometimes, you may run into a situation where you've added binding.pry to the end of a method, but when running learn, it does not pause. There are two potential causes of this:

  1. This issue can sometimes happen if binding.pry is the last line of code in a method like the example below.

    def selection(num)
      if num = 1
        "YUM YUM MUNCH MUNCH MUNCH"
      elsif num = 2
        "HAM HAM HAM IN MY TUMMY"
      end
      binding.pry
    end

    If you've run into this situation, the easiest way to get around this is to add a line of code after binding.pry so that it isn't the last line of code that is interpretted.

    def selection(num)
      if num = 1
        "YUM YUM MUNCH MUNCH MUNCH"
      elsif num = 2
        "HAM HAM HAM IN MY TUMMY"
      end
      binding.pry
      puts 'hello'
    end

    Just make sure to remove this extra code along with binding.pry when running the tests again. Otherwise, your will cause the method to return something unexpected. In the example above, puts 'hello' returns nil, which will, in turn, cause the selection method to return nil when called.

  2. If you've tried the above solution and Pry still does not seem to work, it may be that the method you've placed binding.pry in is never actually called. For instance, if you have the following:

    def runner
      prompt_user
      selection(get_user_input)
      binding.pry
    end

When you run learn, Pry will never work. Why? Because the runner method is not actually called in the tests! The test file spec/false_equivalency_spec.rb has the following in it:

describe "false_equivalency" do
  it "`selection` returns the correct string based on user input" do
    expect(selection(1)).to eq("YUM YUM MUNCH MUNCH MUNCH")
    expect(selection(2)).to eq("HAM HAM HAM IN MY TUMMY")
  end
end

If you recall from the lesson How Tests Work, each expect line is a separate test. These tests are only running the selection method! All the other methods in fix_using_tests/false_equivalency.rb are ignored in this test.

Resources

reading-errors-and-debugging-using-pry-lab-sea01-seng-ft-071320's People

Contributors

maxwellbenton avatar evansjwang avatar sgharms avatar notnotdrew avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

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.