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

SEO

Google follows URLs in text

Today I learned that Google follows URLs even when they are plain text

Web Dev

Check in with your database

It pays to take a step back and look at how your database is set up every so often. You will often find quick wins and hidden tech debt.