Binding irb - Runtime Invocation for IRB

Rohit Arolkar

By Rohit Arolkar

on April 18, 2017

This blog is part of our  Ruby 2.4 series.

It's very common to see a ruby programmer write a few puts or p statements, either for debugging or for knowing the value of variables.

pry did make our lives easier with the usage of binding.pry. However, it was still a bit of an inconvenience to have it installed at runtime, while working with the irb.

Ruby 2.4 has now introduced binding.irb. By simply adding binding.irb to our code we can open an IRB session.

1
2class ConvolutedProcess
3def do_something
4@variable = 10
5
6    binding.irb
7    # opens a REPL here
8
9end
10end
11
12irb(main):029:0\* ConvolutedProcess.new.do_something
13irb(#<ConvolutedProcess:0x007fc55c827f48>):001:0> @variable
14=> 10
15

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.