Reader small image

You're reading from  From PHP to Ruby on Rails

Product typeBook
Published inDec 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781804610091
Edition1st Edition
Languages
Right arrow
Author (1)
Bernard Pineda
Bernard Pineda
author image
Bernard Pineda

Bernard Pineda is a seasoned developer with 20 years of web development experience. Proficient in PHP, Ruby, Python, and other backend technologies, he has taught PHP and PHP-based frameworks through video courses on platforms like LinkedIn Learning. His extensive work with Ruby and Ruby on Rails, along with curiosity in frontend development and game development, bring a diverse perspective to this book. Currently working as a Site Reliability Engineer in Silicon Valley, Bernard is always seeking new adventures.
Read more about Bernard Pineda

Right arrow

Debugging Ruby

Just like with any other significant programming language, Ruby has a number of tools to help us analyze, fix, and improve our code. According to a software architect I encountered many years ago, the disparity between a programmer who possesses debugging skills and one who lacks them equates to the distinction between a junior programmer and a senior programmer. By using the right tools to debug our code, we can both improve our code and improve our understanding of how the language is interpreted, which in the long run, is beneficial to our path as developers. Let’s take a look at the tools that come with Ruby out of the box, as well as the gems that the Ruby community has developed to debug our code.

With debugging in mind, in this chapter, we will cover the following topics:

  • Debugging functions in Ruby versus PHP
  • Gems for debugging
  • Understanding Interactive Ruby (IRB)s usefulness

Technical requirements

To follow along with this chapter, you will need the following:

  • Any IDE to view/edit code (e.g., Sublime Text, Visual Studio Code, Notepad++, Vim, Emacs, etc.)
  • For macOS users, you will also need to have Xcode Command Line Tools installed
  • Ruby version 2.6 or later installed and ready to use

The code presented in this chapter is available at https://github.com/PacktPublishing/From-PHP-to-Ruby-on-Rails/.

Debugging functions in Ruby versus PHP

So far, we’ve made scripts and code snippets making sure that our code works correctly every time. However, in the real world, we’ll come into contact with code that someone else has created and either wasn’t tested, or it wasn’t tested in a scenario that hadn’t come up until now. This happens more often than not, and we should be prepared to get our hands dirty to fix these types of issues. In PHP, we have a couple of functions that will help us debug in the simplest way. You are welcome to just read the following example and not follow along. For now, let’s take a look at PHP’s var_dump() function. We can open a command shell and create a file with the following content:

<?php //buggy_code.php
$person['firSt'] = 'Thomas';
$person['last'] = 'Anderson';
echo "Hi {$person['first']} {$person['last']}";

Let’s say we...

Gems for debugging

As you probably guessed, the Ruby community came across the debugging problem just like any other programming community and thus has come up with libraries (or gems) to encapsulate different debug behaviors. We’ll talk specifically about three gems that make your debugging experience a whole different path than just dumping values in your code:

  • Debug
  • Pry
  • Byebug

All of these gems were designed for the same purpose, but each one has its own peculiarities and it will depend on your own preference which one you choose to use in your projects. Let’s start by looking at the first one.

The debug gem

This gem comes as a replacement (and improvement) to the traditional lib/debug.rb standard library. While there are various ways to use this gem, let’s start by installing our gem and creating a simple debuggable example. First, we will have to do some setup. Let’s use a Gemfile, which we learned about in previous chapters...

Understanding IRB’s usefulness

In Chapter 3, we briefly saw the IRB shell, and I hope you noticed some of the similarities between IRB and our debugging tools. Basically, what these gems do is enhance IRB so that we can view variables in memory, move around execution points, and work in a “frozen” state of our program. At the end of the day, you will be able to choose which one of these tools is more convenient for your everyday use. I could steer you toward one, or you could simply use the built-in IRB. One technique I saw a fellow developer use was that he wrote most of his code in IRB, and once the code ran without problems, he would just copy the code from IRB into an IDE to save his work. This saved him a lot of time that he would have otherwise used on testing. Another coworker used a very popular IDE called RubyMine. This tool allows us to add breaking points at the touch of a button (among many other features). Additionally, one downside of using debuggers...

Summary

In this chapter, we learned about the different functions we can use to debug in PHP and the equivalent in Ruby, and how to debug our code by using three different tools that are easily configured and installed. While not wanting to impose my debugging gem of choice, I will say that for several years I used pry, until byebug came along. I suggest you not only try byebug but also be on the lookout for new debugging gems. We also learned how to add breakpoints to our debug code and how useful and powerful these breakpoints can be during development. Lastly, we learned that all of these gems are basically enhanced IRBs, so we can easily use any of them as they all behave in a very similar fashion.

Having seen all this, we are now ready to board the Ruby on Rails wagon.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
From PHP to Ruby on Rails
Published in: Dec 2023Publisher: PacktISBN-13: 9781804610091
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Bernard Pineda

Bernard Pineda is a seasoned developer with 20 years of web development experience. Proficient in PHP, Ruby, Python, and other backend technologies, he has taught PHP and PHP-based frameworks through video courses on platforms like LinkedIn Learning. His extensive work with Ruby and Ruby on Rails, along with curiosity in frontend development and game development, bring a diverse perspective to this book. Currently working as a Site Reliability Engineer in Silicon Valley, Bernard is always seeking new adventures.
Read more about Bernard Pineda