Which one is better at debugging in rails?
We can start a comparison by a very simple example via rails c
.
binding.irb
example
def sum(a, b)
binding.irb
puts a
puts b
a + b
end
binding.pry
example
def sum(a, b)
binding.pry
puts a
puts b
a + b
end
While binding.irb
has a very interesting promise, like serving as the main debugger in rails, it comes with bugs and drawbacks that simply don't exist when we using binding.pry
for the debugging process.
binding.pry
looks more clear for the debugging process, particularly because binding.pry
outputs to the user the code where binding.pry
is located as soon as the code hits this debugger, while binding.irb
doesn't output anything to the user when the code hits this debugger (even if we call whereami
sometimes it doesn't output anything (😱), which is very confusing in terms of usability).
In addition, there are bugs in binding.irb
related to not outputting the value of a variable when we call it! We can check it by looking at the above pictures and noticing the a variable value was not shown to the user here:
irb(main):004> a
On the other hand, binding.pry
doesn't have this bug and always output the value of a variable when we call it and press enter.
As a last note... Is there a way to fix all of these bugs in binding.irb
? In case not, I would recommend to keep using binding.pry
for the debugging process to avoid headaches.
Markdown version without pictures
binding.irb
Xbinding.pry
?Which one is better at debugging in rails?
Comparison
We can start a comparison by a very simple example via
rails c
.binding.irb
examplebinding.pry
exampleOutputs
binding.irb
binding.pry
Conclusion
While
binding.irb
has a very interesting promise, like serving as the main debugger in rails, it comes with bugs and drawbacks that simply don't exist when we usingbinding.pry
for the debugging process.1. Where am I ?!
binding.pry
looks more clear for the debugging process, particularly becausebinding.pry
outputs to the user the code wherebinding.pry
is located as soon as the code hits this debugger, whilebinding.irb
doesn't output anything to the user when the code hits this debugger (even if we callwhereami
sometimes it doesn't output anything (😱), which is very confusing in terms of usability).2. What are the values ?!
In addition, there are bugs in
binding.irb
related to not outputting the value of a variable when we call it! We can check it by looking at the above pictures and noticing the a variable value was not shown to the user here:On the other hand,
binding.pry
doesn't have this bug and always output the value of a variable when we call it and press enter.Error-Proneness:
binding.irb
appears to be more error-prone because thewhereami
command doesn't show any context, making it difficult to understand where the debugger is paused. In contrast,binding.pry
successfully displays the context, showing the exact location in the code where execution is paused. This indicates thatbinding.pry
is generally more reliable for navigating the code and understanding the current execution state.Intuitive Interface:
binding.pry
has a more intuitive interface compared tobinding.irb
. It provides commands likewhereami
to show the current location in the code and has features such as syntax highlighting, code navigation, and history tracking.binding.pry
offers more advanced debugging tools out-of-the-box, making it easier for developers to inspect variables, control the flow, and understand the code's state.binding.irb
is a basic interactive Ruby shell and lacks many of the helpful debugging features present inbinding.pry
, making it less user-friendly for debugging complex issues.Therefore,
binding.pry
is generally more stable and user-friendly thanbinding.irb
for debugging in Ruby.As a last note... Is there a way to fix all of these bugs in
binding.irb
? In case not, I would recommend to keep usingbinding.pry
for the debugging process to avoid headaches.