Search⌘ K

Debugging Using the Output to the Console

Explore how to debug Ruby code by using console outputs like the inspect method for detailed object representation, raising exceptions to halt execution, and printing stack traces. This lesson helps you understand practical debugging techniques available in Ruby and compares them briefly with JavaScript debugging methods.

The inspect method

This is one of the easiest and most efficient ways to debug a program.

puts something.inspect

We can implement the inspect method for every object in any part of a program. It then returns a string representation of the object. Here, we may ask why we need to use puts something.inspect while we could use puts something.

The answer is that inspect is more verbose, and its primary purpose is to inspect. For example, puts nil and puts "" statements return an empty output on the screen, while .inspect gives us a better idea of what the object is:

$ pry
> puts nil

> puts nil.inspect
nil
>
...