29
loading...
This website collects cookies to deliver better user experience
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
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
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.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
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
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.gem install faker
, and include it in your gemfile.Faker::Movies::Lebowski.quote
Faker::Music::Prince.unique.song
Faker::Games::Minecraft.biome
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.