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.

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.

The 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 require 'irb'.

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 irb session.

This means if you type exit you are only exiting the session generated by Binding.irb, not your own session.


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.