Cobwwweb logo

Add a "reload!" Method to your Ruby Console

Aug 07, 2018 Ruby

I wrote another article on creating a console session for your ruby project. That is a powerful method for working with your (non-Rails) Ruby project on the fly. The problem with that approach is that you have to restart the console every time you make a change because the console has stored the state of your codebase when it started.

In the Rails console, there is a reload! method that is meant to reload your application code in the current console session. It's a fairly complex process for Rails, but when you have a much simpler project, all you have to do is reload all your files and you're good to go.

In the previous article, our console script looked like this:


#!/usr/bin/env ruby

require 'irb'
require_relative '../lib/calculator'


We can add a reload! method directly in this script that will reload every file within the lib directory.


#!/usr/bin/env ruby

require 'irb'
require_relative '../lib/calculator'

def reload!(print = true)
  puts 'Reloading ...' if print
  # Main project directory.
  root_dir = File.expand_path('..', __dir__)
  # Directories within the project that should be reloaded.
  reload_dirs = %w{lib}
  # Loop through and reload every file in all relevant project directories.
  reload_dirs.each do |dir|
    Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
  # Return true when complete.


Let's see if it works. Using the previous calculator example, let's open a new console session:

$ bin/console

Then let's try to use our calculator class to multiple two numbers:

irb(main):001:0> calc =
=> #<Calculator:0x00007fbdae8d4eb0>
irb(main):002:0> calc.multiply(2, 3)
Traceback (most recent call last):
        2: from ./bin/console:20:in `<main>'
        1: from (irb):2
NoMethodError (undefined method `multiply' for #<Calculator:0x00007fbdae8d4eb0>)

It doesn't work because we don't have the multiply method written on the Calculator class. So let's do that without closing the console session.


class Calculator

  # ...

  def multiply(n1, n2)
    n1 * n2


And within the console session, run the reload! method:

irb(main):003:0> reload!
Reloading ...
=> true

And now try to multiply (note that you can do this without reinstantiating your calc variable):

irb(main):004:0> calc.multiply(2, 3)
=> 6

It works!

Now you are free to continue building on your project without having to restart the console session each time you make a change.

Did you learn something or find this article interesting?

If so, why not