💎Ruby Tip💎 Interactive debugging with out the necessity for gems.
Andrés
•
20 November 2023
Uncover a easy and quick solution to debug in Ruby with out putting in further gems. With the Binding class and the built-in IRB console, you may discover and modify the execution context to resolve errors effectively.
There are totally different standard gems within the Ruby universe with numerous functionalities and totally different syntaxes to carry out interactive debugging. A few of these gems may be byebug or debug. The issue with these gems is that generally they have to be put in, configured and have their very own instructions to study. This takes a while and plenty of occasions there isn’t any want for one thing so complicated for such a small bug.
For these instances, we can have the choice of utilizing the Binding class. This enables us to encapsulate the execution context at a given level and return it for future use. Binding objects may be created by calling the Kernel#binding
methodology and the console might be raised utilizing the general public occasion methodology irb
.
With slightly little bit of code it is going to be greater than clear:
# door.rb
class Door
def initialize
@open = false
binding.irb
places "Is the door open: #{@open}"
finish
finish
Door.new
Operating our small script will open an IRB session with which you’ll evaluation the context and modify it:
Documentos/scripts/ruby through 💎 v3.2.2
❯ ruby door.rb
From: door.rb @ line 4 :
1: class Door
2: def initialize
3: @open = false
=> 4: binding.irb
5: places "Is the door open: #{@open}"
6: finish
7: finish
8:
9: Door.new
irb(#<Door:0x00007fa9a0f367a8>):001> @open
=> false
irb(#<Door:0x00007fa9a0f367a8>):002> @open=true
=> true
irb(#<Door:0x00007fa9a0f367a8>):003> exit
Is the door open: true
And that’s it, you need to use it to debug your scripts, internet scrappers or no matter you might be constructing.
To study extra about utilizing IRB you may go to this documentation
In case you preferred it be happy to say hello within the feedback, I’ll be watching.
Joyful coding!