Is there any way to set a break point for debug in the environment in which gems like binding.pry are not installed?

Viewed 408

From this April, I participated in a new project in which gems for debug are not installed and I am not allowed to install new ones.

I would like to set a break point and see the inside of variables like binding.pry. I googled to debug in such a way without newly installing gems or by using extensions of VScode, but most of the pages require me to install ones.

It would be highly appreciated if you would share with me your ideas to debug in the way.

== Additional Information on April 8th, 2020 ==

Ruby version is 1.8.7. Rails version is 2.3.5.

1 Answers

Unfortunately, Ruby doesn't really have a built-in breakpoint-based debugger in that sense.

The closest thing you can do is start an IRB session mid-program, which halts it and allows you to inspect the current state of the program by evaluating Ruby commands. This is available in Ruby 2.4 and above as binding.irb. (There are docs available for this on the Binding class.)

IRB doesn't let you step forwards like Pry with Pry-Nav does, but you can just insert binding.irb expressions in as many places as you need, and exit from IRB to "step" to the next one.

Take ths simple program. I want to check the value of those three variables I've assigned, so I put binding.irb there:

puts "=== Hello! ==="

a = 1
b = 2
c = 3

binding.irb

puts "=== Goodbye! ==="

Upon running this program, Ruby will give you an IRB prompt after it's evaluated c = 3, where you can run Ruby code to check values and whatnot (type exit to leave):

=== Hello! ===
From: test.rb @ line 7 :
    2: 
    3: a = 1
    4: b = 2
    5: c = 3
    6: 
 => 7: binding.irb
    8: 
    9: puts "=== Goodbye! ==="
irb(main):001:0> a
=> 1
irb(main):002:0> b
=> 2
irb(main):003:0> c
=> 3
irb(main):004:0> exit
=== Goodbye! ===
Related