How to Debug Your Code in a Simple Fun Way
Debugging your code as a new programmer may seem like a daunting task. However, there are certain tricks that can be used to make the process simple and fun- at least during the early stages of debugging. One such trick coders employ is to write puts statements inside each part of their programs to evaluate how the program runs at different stages. By doing this, we can take a snapshot of what each piece of the code is evaluating as it runs and try to catch any mistakes along the way.
Let’s demonstrate how this process works with an example. Let’s pretend we need to write a method that outputs the longest word in a given phrase or sentence. The initial (buggy) code looks like this:
def find_longest_word(input)array = input.splitlongest_word = array[0]array.each do |element|
if longest_word.length < element.length
longest_word = element
end
end
return longest_wordendp find_longest_word("What is the longest word in this sentence?")
This method seems to work fine at first glance. However, after running it we get the following output:
The code seems to be taking into account the “?” sign at the end of the given sample string (since the sample input string happens to be a question, not a regular sentence). If the phrase given is a question with a question mark at the end, the program believes that the “?” character is part of the last word of the input, and may therefore return the wrong longest word.
In order to see what’s going on under the hood, let’s use puts debugging as we execute the method to see where we’re going wrong. We can add puts or p commands in strategic places along the code as follows:

After typing the puts commands, we can run the code and see the following result:

As we can see, the p array command added on line 5 of our method, prints the elements of our input sentence separated into words. This very first line of printed elements shows us that the last element of the input array is already including the “?” sign as part of the word “sentence”. Therefore, we know that the error of including the question mark is happening at the very beginning of our code.
By knowing where the erroneous logic begins, we can make adjustments accordingly and debug the code. In this example, we could easily do so by adding a few lines of code before we compare the words to take care of the “?” character inclusion in our list of words.
The correct code looks like this:
def find_longest_word(input)
if input.end_with?"?" #fix for our bug
input = input.chomp("?") #fix for our bug
end #fix for our bug
array = input.splitlongest_word = array[0]array.each do |element|
if longest_word.length < element.length
longest_word = element
end
end
return longest_wordendp find_longest_word("What is the longest word in this sentence?")
The new (correct) output is now:
It now prints the correct longest word, without taking into account the “?” character in the given input.
This is how we can use puts statements to debug our code in a simple and fun way!
