Debugging and Troubleshooting Tips for Ruby Programs - dummies

Debugging and Troubleshooting Tips for Ruby Programs

By Christopher Haupt

Ruby is a pretty flexible language, and it gives you a lot of tools to write amazing programs with very little work. However, new Rubyists quickly learn that with all that power comes a bit of responsibility to adequately test and debug your code carefully. Fortunately, there are some simple techniques you can use to help root out problems in your code.

Check the terminal

Most Ruby programs are run from a command line using your platform’s terminal or console program. Ruby is an interpreted language, which means that as you run the program, Ruby is seeing your code for the first time and finding issues as they come up. This is different from some other languages that have a step called compilation, which can often find problems before the program runs.

Because your program is interpreted on the fly, you really need to test all parts of it carefully. If Ruby finds an issue, it will print out a warning or error message to the terminal. Most of the time, that error message will include a description of the problem and an approximate location where the problem occurred.

The information in that error message is your first clue as to where to look for bugs.

Ruby tries to help

The error messages Ruby displays come in a variety of types. When you understand the basic categories, it’s much easier to track down the problem:

  • NameError messages are very common and are usually encountered when you have a typo in your program. If you try to use a variable or method, but type in the wrong name, Ruby won’t be able to find it and will display this error. Look carefully in your code for any naming mistakes. Remember that Ruby is case sensitive, so your variable might be spelled correctly but accidentally have a lowercase or uppercase letter in it.

  • SyntaxError messages happen if you accidentally make a mistake in your Ruby code, usually my missing or misusing a Ruby keyword. For instance, if you forget the end keyword at the end of an if condition, Ruby will let you know. Look in the area of the syntax error for missing code or code that got jumbled up while editing.

  • NoMethodError messages mean that you’re trying to use an object’s method, but it isn’t there, possibly because you spelled it incorrectly. When you get this message, check that the method exists in your source code and that your spelling is correct.

There are a number of other possible errors, but these three are some of the most common ones.

Use “puts” to create breadcrumbs

Sometimes your program acts oddly but Ruby doesn’t display an error message. This is most often due to a coding mistake that programmers call a logic error. This means the code doesn’t do what you intended it to do.

Logic problems are hard to track down, but one simple tool is to print out debugging messages from your program at various points along the path of your code. If you use the Ruby puts method, your messages will display to the terminal while you run the program. These message may give you some hints of where the code is running at any given moment and what its variables contain. You can write lines like puts now in my method or puts instance variable = #{@my_variable} to get a quick feel for the program’s behavior.

Ask for help

Sometimes you can stare at a program for a long time and not see the simplest mistakes right in front of you. A tried-and-true technique in these cases is to set aside the program and come back to it later with a fresh set of eyes.

If that doesn’t work, there is a lively community of Ruby programmers on the Internet, and doing some quick Google searches will often lead to an answer.

If all else fails, finding another programmer who you can ask a question to, in person or online, is a great approach.