Ruby Error Messages a.k.a Sloth Fratelli

a Ruby programmer’s best friend


Alright, I’ll be the first to admit that I don’t exactly remember everything that happened in the movie The Goonies and if you stop reading right here I’m incredibly sorry. One thing I do remember is there was a guy who looked nightmarishly scary and horrifying but ended up being one of the most awesome characters in the movie. His name was Lotney “Sloth” Fratelli. And he was a boss.

For those that don’t remember, this is Sloth Fratelli

Like I said before, Sloth is portrayed as this horrifying monster who the kids are trying to avoid. By the end of the movie (***SPOILER ALERT***) Sloth is wearing a pirate’s hat and ripping off his dull brown shirt, revealing the most 90's style Superman shirt this world has ever witnessed.

Sloth Fratelli in all of his honor and glory

When I first started programming in Ruby, error messages were the exact same way. I would misspell a variable, forget to put an end, do something I saw as relatively small, and Ruby would send an error that made me feel like I was just struck by the Ruby gods. Then I saw the light…I actually decided to see what these seemingly long and hateful messages were actually telling me and they ended up being as “ruby-esque” as basically every other aspect of Ruby.

Ruby has a few different types of error messages for different types of errors, all of which have different meanings that basically point out exactly what your error was and where it occurred. For the purpose of this blog, we are going to talk about a basic error and show what exactly it is telling us (if you would like to see all standard Ruby Error Types, visit here).

Here is an example of a basic Ruby error message:

lib/greeting_the_world.rb:3:in `<main>': undefined local variable or method `hello_world' for main:Object (NameError)

At first glance, this can seem pretty intimidating. But like almost everything else in programming, breaking up the message into parts can make your life millions of times easier.

lib/greeting_the_world.rb:3:in `<main>':

This is saying exactly what file and where in the file the error occurs. The error occurs in the file named “lib/greeting_the_world.rb” on line 3. Seriously? Did Ruby just tell us exactly what line the error is on? Thank you, Matz. “<main>” is the scope that the error occurs in (if this means nothing to you as a beginner, here is a good little explanation of Ruby scopes). Lets keep trekking on.

undefined local variable or method `hello_world' for main:Object

This is Ruby’s way of telling you what it thinks went wrong. Ruby is (usually) pretty good about telling you why your code just broke. An error like this would occur if you would try to call a variable “hello_world” when that variable doesn’t exist. You will most likely see a lot of these. On to the last part.

(NameError)

This is a pretty informative part of the error. This shows you the type of error that it was. A NameError is called when a given name is invalid or undefined, which is pretty much what Ruby was trying to tell us earlier in the message. Like I said before, all of the standard Ruby error types can be found here. If you ever see an error and you have no idea what it is saying, go there. It’s there for your happiness.

Hopefully after reading this you can see that error messages in Ruby are not there to hinder your development process, but to make it as painless as it can possibly be. They seem ugly, but they are really there patting you on the back encouraging and guiding you on how to proceed. Understanding how to read error messages can greatly enhance your programming experience and effectiveness.