As a core part of the Ruby ecosystem, IRB (Interactive Ruby) is an invaluable tool for developers. With its rapid pace of changes and improvements, staying up-to-date with the latest features can significantly enhance your development workflow. In this post, we’ll cover the significant updates to IRB from the first half of 2024 (between v1.11.0 and v1.14.0), as well as enhancements in the Rails Console.

(You can also read the improvements we made prior to v1.11.0 in my previous article, “Unveiling the big leap in Ruby 3.3’s IRB”.)

NOTE

To upgrade IRB, you can simply install it as a gem by adding gem "irb" to your Gemfile.

What’s New

Help Command and Help Message Improvement

General Improvements

A good help command is crucial for usability. The help command in IRB has been significantly improved.

  • Previous Behavior: Opens an ri console to look up Ruby API documentation.
  • Current Behavior: Displays help messages for IRB itself, aligning with user expectations.
  • API Documentation: Users can use show_doc for API documentation.

Detailed Help for Specific Commands

The new help <cmd> feature allows tools to provide detailed usage information, improving feature discovery.

Examples

demo of the help command

irb(main):001> help edit
Usage: edit [FILE or constant or method signature]

Open a file in the editor specified in ENV["VISUAL"] or ENV["EDITOR"]

- If no arguments are provided, IRB will attempt to open the file the current context was defined in.
- If FILE is provided, IRB will open the file.
- If a constant or method signature is provided, IRB will attempt to locate the source file and open it.

Examples:

  edit
  edit foo.rb
  edit Foo
  edit Foo#bar

Extension API

With IRB v1.13.0, a new API was introduced to extend its functionality. This allows libraries to customize and enhance IRB sessions by adding new commands and helper methods tailored for specific needs.

Helper Methods vs. Commands

  • Helper Methods: Designed to return Ruby objects that interact with the application
    • For example, a admin_user method that always returns the admin user object
  • Commands: Perform tasks or display information, similar to shell commands.
    • For example, IRB’s show_source and edit commands

Real-world Examples

For more details and examples about the API, please visit its documentation.

New Commands

The disable_irb Command

The disable_irb will disable any further binding.irb calls for the session, making it easier to use breakpoints, especially in loops or frequently called code paths.

Primary Use Case

For users of binding.irb, there were two ways to exit a breakpoint:

  1. Use exit to leave the current breakpoint.
  2. Use exit! to end the entire process.

In code paths executed multiple times (e.g., within loops), repeatedly using exit could be cumbersome. The disable_irb command performs an exit from the current breakpoint, and at the same time disables all subsequent breakpoints within the session, so the program can immediately resume its operation.

Example

demo of the disable_irb command

The cd Command

The new cd command simplifies navigating through different contexts within an IRB session. Users can use cd <obj> to make <obj> the current context, and then cd .. to move to the previous context.

Please note that it doesn’t match Pry’s cd command and doesn’t support usages like cd @x/@y or cd -.

Example

# test.rb
class Foo
  def bar
    puts "bar"
  end
end

f = Foo.new

binding.irb

demo of the cd command

Help message

Usage: cd ([target]|..)

IRB uses a stack of workspaces to keep track of context(s), with `pushws` and `popws` commands to manipulate the stack.
The `cd` command is an attempt to simplify the operation and will be subject to change.

When given:
- an object, cd will use that object as the new context by pushing it onto the workspace stack.
- "..", cd will leave the current context by popping the top workspace off the stack.
- no arguments, cd will move to the top workspace on the stack by popping off all workspaces.

Examples:

  cd Foo
  cd Foo.new
  cd @ivar
  cd ..
  cd

Improvement on Existing Commands

The show_source Command

The show_source command has received significant updates, making it more powerful and user-friendly.

Enhancements

  • Display Methods Defined During IRB Session: Previously, show_source couldn’t display methods defined within an IRB session. This limitation has been addressed.

    irb(main):001* class Foo
    irb(main):002*   def bar; end
    irb(main):003> end
    => :bar
    irb(main):004> show_source Foo#bar
    From: (irb):2
    def bar; end
    => nil
    

Bug Fixes

  • Handling Top-Level Constants: Fixed issues related to top-level constants like ::Post.
  • Improved Constant Lookup Logic: Enhanced reliability and usability.
  • Detailed Help Message:

    Usage: show_source [target] [-s]
    
    -s  Show the super method. You can stack it like `-ss` to show the super of the super, etc.
    
    Examples:
    
    show_source Foo
    show_source Foo#bar
    show_source Foo#bar -s
    show_source Foo.baz
    show_source Foo::BAR
    

irb:rdbg Sessions

The irb:rdbg sessions provide an integrated debugging experience within IRB.

Bug Fixes

  • Fixed issues related to history saving and input recognition, enhancing reliability and usability.

Repeating Previous Command Execution

  • Sending blank input after certain commands, like c (for continue), will automatically repeat the command. This matches debug’s REPL behavior, making the integration more convenient.

Additional Information

  • Users can use the RUBY_DEBUG_IRB_CONSOLE=1 to replace debug’s default console with irb:rdbg. This allows users to keep using debugger or binding.break while still benefiting from an irb:rdbg session. For more information about irb:rdbg, please refer to the IRB’s GitHub README.

Rails Console Improvements

Rails Specific Prompt

The Rails console now displays a Rails-specific prompt format.

The prompt format is <app-name>(<env>)>, such as my-app(prod)>. This enhancement helps developers quickly identify the application and the environment they are working in, reducing context-switching errors.

Integration with IRB’s Help Message

Rails commands and helper methods are now integrated into IRB’s help message system, making them easier to discover and use within an IRB session.

Example

my-app(dev)> help
...

Rails console
  reload!        Reloads the Rails application.

Helper methods
  conf           Returns the current IRB context.
  helper         Gets helper methods available to ApplicationController.
  controller     Gets a new instance of ApplicationController.
  new_session    [Deprecated] Please use `app(true)` instead.
  app            Creates a new ActionDispatch::Integration::Session and memoizes it. Use `app(true)` to create a new instance.

...

Recap

The first half of 2024 has brought some fantastic improvements to IRB. From a more intuitive help command to a powerful new extension API, IRB is evolving to make Ruby developers’ lives easier. New commands like disable_irb and cd simplify common tasks, while enhancements to show_source and irb:rdbg sessions make debugging smoother.

Rails console hasn’t been left behind either. With a new Rails-specific prompt format and better integration of commands and helper methods into IRB’s help system, it’s now easier to identify your environment and discover useful commands.

Finally, I want to give a big shoutout to the community contributors who made these enhancements possible. Dive in and explore these new features, and stay tuned for more updates at the end of the year!