When to include irb in Ruby 2.4.0
When should you include irb into your scripts and when you don't need to in Ruby 2.4.0
I’ve only just got around to playing with ruby 2.4.0 which was released in late 2016. One of the new features of the language is introduced was
Binding.irb allows you to pause execution of a script and play about in an
irb session. This brings some of the features that people had to use something like Pry for before into Ruby.
Depending on the article you read some folk will start talking about
Binding.irb with a code sample with
require 'irb' at the top and others won’t.
I wanted to very quickly clear this up.
irb functionality in Ruby is baked into the language, but just like other classes (the
CSV class, for example) you need to require it should you want to use it.
This means if we want to run our script by typing
ruby my_script.rb we will need to have a
If however we are running our script from inside an
irb session already, then we don’t need to include it. Ruby when executed from inside an
irb session already loads in the relevant classes.
A note about running Binding.irb from inside IRB
If your script is being ran inside of
irb, for example by running
irb -r './my_sript.rb' when you hit
Binding.irb it will pause execution and effectively open up an
irb session inside your
This means if you type
exit you are only exiting the session generated by
Binding.irb, not your own session.