DEV Community

Cover image for binding.pry by many programming languages
Hiromichi NOMATA
Hiromichi NOMATA

Posted on • Originally published at algonote.com

binding.pry by many programming languages

Comparison of process interruption tool

Image description

What is binding.pry?

While debugging, the engineers sometimes want to see the variable values at that point. If your language is static and you use IDE, you can set break point by GUI tools. However, if you use a scripting language with a pure text editor, we cannot take it the same way.

Open REPL and run each line might be possible, but it's a hassle.
On Ruby, inserting binding.pry or binding.irb can launch REPL at a specific point. I share how we can do the same thing for the other languages.

Ruby

After gem install pry

require 'pry'
a = 1
b = 2
binding.pry # open REPL
puts a+b
Enter fullscreen mode Exit fullscreen mode

Since Ruby 2.4, native irb supports similar feature

require 'irb'
a = 1
b = 2
binding.irb # open REPL
puts a+b
Enter fullscreen mode Exit fullscreen mode

Python

pip install ipython

from IPython import embed
a = 1
b = 2
embed() # open REPL
print(a+b)
Enter fullscreen mode Exit fullscreen mode

Node.js

Style is different. node inspect test.js

a = 1;
b = 2;
debugger; // breakpoint
console.log(a+b);
Enter fullscreen mode Exit fullscreen mode

At launch time, the cursor is on the head of the script. Move to the breakpoint.

debug> cont // Continue to breakpoint
debug> repl // Open REPL
Enter fullscreen mode Exit fullscreen mode

PHP

composer require psy/psysh

<?php
require 'vendor/autoload.php';

$a = 1;
$b = 2;
eval(\Psy\sh()); // open REPL
echo $a+$b;
echo "\n";
Enter fullscreen mode Exit fullscreen mode

Impression

Node.js's style is unique.

Top comments (0)