DEV Community

Eisea904
Eisea904

Posted on

A Common Error and a Debugging Tool, end & pry

When starting Ruby, there are common errors you will undoubtedly encounter. Here we will look at the most common of these and a powerful debugging tool, pry.

Example 1
Alt Text

One of the most common errors will look like this:

blog_examples.rb:10: syntax error, unexpected end-of-input, expecting end'

Let’s dissect this…

file_name:line_that_returns_error: error_type, details

This error is telling us that our file is missing an end. The part that may confuse beginners is that it is line 10 that gives us the error. This may be misinterpreted as "line 10 needs an end." Actually, it is telling us that by the end of the file (line 10) it was expecting at least one more end than is present. And now we know that one of our methods is missing an end. Looking through, we can see that #gossip(name) needs this fix.

Example 2
Alt Text

The sister error to this is
blog_examples.rb:8: syntax error, unexpected end', expecting end-of-input

There is now an extra end that we forgot to delete when we deleted #say_goodbye!

Pry is an invaluable tool in debugging, but it can also be confusing so let's talk about how to use it and what it can and can't do. Firstly, unless you know it's included elsewhere in your environment, add require 'pry' to the first line of any file you want to use it in. Go to the line or end of the section you want to test, hit enter/return to make a blank line and there put binding.pry .

When this program is run, it will pause at that line and be able to access everything within its scope that came prior. The binding.pry will not be hit if that piece of code is not run. If you are using it within a method, then at the end of your code you need to call that method to run it. In the example above we call on #say_goodbye at the end with say_goodbye(John) . If we put a binding.pry inside of the #say_goodbye method, it will only now be hit. Pry can also only access information within its scope, so within a pry session, we cannot access variables defined within other methods, for example.

Once pry is working, you will see in your computer's terminal the word pry and a colon. Here you can type ideas for the following lines. It's a sandbox, so instead of deleting your code to try an idea and then saving it only to delete that and try your next idea (and so on...) we can simply type ideas in the pry session. When we are done, type exit to go up one layer toward your normal terminal (and usually pry sessions are only one layer deep). exit! will always take you completely out of a pry session.

In example 3 our binding.pry can be used to check what is being passed as the variable name_as_string. When you type name_as_string into the pry session in your terminal, it should give us "John". However, anything inside the prior method #say_hello is outside the scope of #say_goodbye , so we cannot access those things while pry is on its current line.

Example 3
Alt Text

A note on pry, it will not "tell you" what is wrong with your code. It will however save you time and give you your error messages much more quickly than if you had to change your code within the file and save with each attempt or idea.

Top comments (0)