Reading error messages is really important.

As beginning Ruby programmers, too often our process is as follows:

1. Notice you’re getting an error.
2. Don’t read or comprehend the details of the error.
3. Make an immediate assumption about the cause.
4. Change something in your code and hope it will help.
5. Run the test again and see if it fixed it.
6. Repeat steps 1–5 a few times.
7. Notice you’re not getting anywhere.
8. Start to think about actually reading the error.
9. Read the first part of the error.
10. Repeat steps 4–5.
11. Repeat steps 4–5 again.
12. Notice you’re STILL not getting anywhere.
13. ACTUALLY READ THE ENTIRE ERROR.
14. Be like, “….oh.”

A surprising amount of times, at this point you’ll know exactly why you’re getting the error and how to fix it. It’s then just a matter of getting your coding hat on for a little bit, and making it work.

But here’s the thing.. You could skip steps 1 through 12!

Every. Single. Time. you have an error you’re trying to fix, you could start at step 13. Think of how much wasted time will be saved!

So, here’s a better way to deal with errors:

1. Read the entire error.
2. Understand the error.
3. Think about what the error is telling you.
4. THEN act.

Here’s an example I ran into just the other day:

Let’s say we’re trying to run the following code:

forms.each do |f|

end

And we’re getting the following error:

forms.rb:5:in `<main>': undefined method `each' for nil:NilClass (NoMethodError)

1. Read the entire error.
So, let’s take a look! Errors in Ruby are your friends. Don’t let them get you down. Errors are to be celebrated! They’re actually really nice to us. Look at all the info they give us!

forms.rb:5

This is the name of the Ruby file, and the line number we were on when we hit the error.

:in `<main>’

The current scope. If we were inside a method, object, etc., this would change accordingly.

undefined method `each’ for nil:NilClass (NoMethodError)

Okay, so here’s the actual error message. Let’s break it down even further:

2. Understand the error.

undefined method ‘each’

It’s trying to run a method called ‘each,’ but that method doesn’t exist. Why?

for nil:NilClass

If you open up IRB and ask it

nil.class

you can see that NilClass is actually the class that nil is.

2.3.1 :001 > nil.class
=> NilClass

So, putting it all together, it’s telling us a few things:

1. You’re trying to run a method called ‘each.’
2. The object you’re trying to run it on is nil, which is a member of NilClass.
3. For objects of the class NilClass, there is no method called ‘each.’

Looking back at the line of code you wrote on line 5,

forms.each do |f|

what are we trying to run the ‘each’ method on? A variable called forms.

So what the error is telling us is, forms must be nil, and nil doesn’t have an ‘each’ method, so of course I can’t run ‘each’ on it!

There you have it. We now have a clear direction to go in.

  • Where was the variable ‘forms’ assigned?
  • How did it get to be nil?
  • Has it always been nil?
  • If not, when did it change?

These are good next questions to explore.

So you see, by pausing a moment and really looking, we’ve saved a ton of time and avoided a lot of blind poking around. And we’re a lot closer to fixing the error.