michael-neis
Posted on January 7, 2022
If you have started working with Ruby or Ruby on Rails, you are probably familiar with the fact that the language itself was built for making a programmer's life easier. You may also already be familiar with some of the helpers that you can install to make it even easier. I've discovered a handful of helpers, or 'gems' as they are called in Ruby, as I've worked in the language, and hopefully some of these gems can prove helpful to you.
Byebug and Pry
As far as debugging tools go, the Byebug and Pry are two of the best gems I have found. They are used in different contexts, but for all intents and purposes, including them in your code will halt an operation and allow you to access whatever point in the code you halted within your terminal with a REPL.
For Pry, you simply need to install it with gem install pry
and make sure the gem is included in your gemfile. Pry is considered an IRB alternative to allow developers runtime access within their console. Here's an example of it's usage:
require 'pry'
class Greet
greeting = 'Hello'
person = 'Bob'
puts '#{greeting}, #{person}!'
end
binding.pry
In order to use Pry, first we must require it with require 'pry'
. Now, wherever we decide to place binding.pry
will be the point at which our code halts. In this case, running this file will open a REPL in our terminal with the Greet
class fully loaded, so we are able to access it and see if it is operating how it should. Alternatively, we could place the pry here:
require 'pry'
class Greeting
phrase = 'Hello'
binding.pry
person = 'Bob'
puts '#{greeting}, #{person}!'
end
Now when we run the file, the code will halt within the class Greeting
, and once again we will have access to a REPL. In this case, however, because person
has not yet been assigned, we will only have access to phrase
within our terminal. This is a helpful way to break apart any classes or methods that may be failing to find exactly where it is that our code is bugged.
Byebug works in a very similar way, but is used for adding bindings to files that are being invoked in an active server. Install it with gem install byebug
and make sure it is included in your gemfile. Byebug is extremely helpful for debugging Ruby files that are being called upon by front end languages such as JavaScript. Let's say we had a greetings_controller
that was being used to handle fetch requests to the '/greetings' POST route. Here's an example of how we could debug it:
class GreetingsController
def create
new_greeting = User.create(phrase: params[:phrase], person:
params[:person])
byebug
render json: new_greeting, status: :created
end
end
Right now, the byebug would halt the code and open a terminal, in which you would be able to access new_greeting
. But let's say there's a problem with new_greeting
, for some reason it cannot be defined, and we still have an error. We could add our byebug a little earlier:
class GreetingsController
def create
byebug
new_greeting = User.create(phrase: params[:phrase], person:
params[:person])
render json: new_greeting, status: :created
end
end
Now, we are able to check in our terminal what exactly are being passed in with params
. If the data object does not contain phrase and person key value pairs, we know that something must be wrong with the way data is being passed. Byebug is incredibly helpful in this way in determining whether or not the error with the data is happening on the front end or the back end.
Faker
If you are working on a new project in Ruby and want to seed it with data, but don't have the time or energy to manually seed every piece of data, Faker is a great gem. Install it with gem install faker
, and include it in your gemfile.
Faker is a gem with an impressive library of data that you are able to access at random when invoking it in your seed data. From random names, to pop culture references, to game titles and more, Faker is able to fill your database with all kinds of data. Here's a few examples of it's use:
Faker::Movies::Lebowski.quote
Faker::Music::Prince.unique.song
Faker::Games::Minecraft.biome
As you could imagine, each of these lines pulls a Big Lebowski quote, Prince song, and Minecraft biome at random from the Faker database. Using a unique
tag in front of the Prince song will ensure that no song is used twice in the seeding. Running .clear
will clear the uniqueness tag, allowing repeats again.
Hopefully these few gems will help in your progress as a Ruby developer. New gems are always being created, so keep an eye out for gems that you might find helpful.
Sources
Posted on January 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.