ruby-on-railsruby-on-rails-7rails-console

How to alias `reload!` in rails 7.2 console?


I had an alias for reload! command in the rails console:

# ~/.irbrc

if defined? Rails
  def r
    reload!
  end
end

In rails v7.2 this no longer works:

>> r
(stackoverflow):1:in `<main>': undefined method `reload!' for main (NoMethodError)

    reload!
    ^^^^^^^

but reload! still works on its own:

>> reload!
Reloading...

How do I aslias reload! with r?


Solution

  • The future proof way seems to be IRB extensions.

    IRB::Command.register(:r, Rails::Console::Reloader)
    

    The entire console integration was changed in Rails 7.2 and a lot of the exisiting functionality moved into rails/railties/lib/rails/commands/console /irb_console.rb.