Debugging Code for Beginners (Ruby)

Ever spend a few hours staring at the small bit of code wondering what the hell happened? I know I have, but through patience and careful analysis, I learned how to quickly get to the root of the bug and move past it. If you are thinking, “I need to learn to code, not debugging.” you are wrong. Bugs are normal in development, they can be some of the most demoralizing situations. Although you do not need to spend hours learning to debugging.. you should spend some time getting familiar with errors and some tools to help you problem solve.

I will give you some tips on how to prevent bugs from the start, and of course when you get the inevitable Nil:Class how to find the issue and fix it quickly.

Stages of Debugging:
1. Where?
2. How?
3. Impossible!
4. Oh….

Debugging program: Ruby: Pry

Pry is a Ruby gem that is used to halt the execution of your code and allow you to enter an IRB state so you can check values and even enter new code to see how the program will respond.

Use Scenarios:

After requiring the gem in your code, you can insert binding.pry anywhere in your code. When the program runs, it will stop where the bind exists and allow you to enter new code. This code will be processed and all values are returnable, but none of the code will be saved in your source code.

General Guidelines:

  • Keep it simple… If your methods or functions don’t have a clear objective or have multiple unrelated objectives, break them up. Having large methods can be difficult to troubleshoot as the interpreter won’t be able to drill down as far to give you good feedback.
  • Not sure what is going to happen? Test it first, not later. It is also a good way to learn and re-enforce new concepts. Also if you make an assumption that is wrong, it can start pushing your code in a direction that you may not be able to salvage later on when you have to repair bugs.
  • As a beginner, don’t copy and paste code! (even if it is your own) If you do, keep a comment or remember that you did, because it almost always leads to a bug!

Alright time to fire this new program up… this is gonna work, and it’s gonna be awesome!!!! Wait… what?!!

undefined method `print_data’ for nil:NilClass

You think to yourself, how could this be? I remember creating this method and I definitely called it on something on the proper class.. or did I? Now that you have the pry gem, you drop a binding.pry before the method call, check the value and….. oh snap, its nil. Looking further back it seems you didn’t create the value you thought that nested hash would create. You can either try again, or to be sure you get it write this time, drop a binding.pry in the loop and play around into you get the values you want.

Debugging Tips:

This might seem silly but, actually read the error messages. The interpreter can be a powerful tool for debugging.. “listen” to it. Here are some common error messages and what they mean.

  • undefined method `print_data’ for nil:NilClass

You called a method on a value that is nil (aka nothing/ undefined)

  • undefined method `+’ for String:Class

You are trying to add non-integer values or have a mismatch in value types (ie. String + Object). This goes for all math operators ( +, — , * , / , % )

  • undefined local variable or method `my_variable’ for main:Object (irb):12:in `<main>’

You have called a variable or method that is either undefined or is not in the current scope of your call. Also, take note the interpreter gives you the location of where the code broke. Start there!

I hope you found this post useful. If you want more interesting articles or build ideas, check me out on twitter Eric Hasselbring.