Debugging Ruby
Debugging by reading the stack trace
it's when you get an error at runtime , this is the easiest form of error you can get in development , you will always be given the error name and at what line it occurred with a brief explanation of the problem .
Debugging with puts
or p
When the code works and you get unexpected results then the easiest and quickest way to confirm your assumptions while debugging is by using puts
.
Don't use puts because if you debug anything that is nil
or empty then it will give you a blank line , unlike p
, p
will tell you that it's nil
example :
puts "Using puts:"
puts []
p "Using p:"
p []
Debugging with Pry-byebug
an easier way of debugging is by using pry-byebug , after you install it , you to add
require 'pry-byebug'
at the top of you script and the call binding.pry
as if it's a break-point somewhere in you program 🙂 , after you run your code you will be given something like IRB in your terminal to debug your code , keep in mind is that you stoped your program at that point anything after it is unknown for the debugger .
you can find usual debugging commands like next and break here at https://github.com/deivid-rodriguez/pry-byebug?tab=readme-ov-file#commands
Debugging using VScode rdbg Ruby Debugger
create a launch.json for your project and select Ruby(rdbg) when prompted , it's like chrome developer tools but for ruby .
read more about it here :
https://github.com/ruby/vscode-rdbg?tab=readme-ov-file#readme
Top comments (0)