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.
A good help command is crucial for usability. The help
command in IRB has been significantly improved.
ri
console to look up Ruby API documentation.show_doc
for API documentation.The new help <cmd>
feature allows tools to provide detailed usage information, improving feature discovery.
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
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.
admin_user
method that always returns the admin user objectshow_source
and edit
commandsFor more details and examples about the API, please visit its documentation.
disable_irb
CommandThe 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.
For users of binding.irb
, there were two ways to exit a breakpoint:
exit
to leave the current breakpoint.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.
cd
CommandThe 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 -
.
# test.rb
class Foo
def bar
puts "bar"
end
end
f = Foo.new
binding.irb
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
show_source
CommandThe show_source
command has received significant updates, making it more powerful and user-friendly.
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
::Post
.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
SessionsThe irb:rdbg
sessions provide an integrated debugging experience within IRB.
c
(for continue), will automatically repeat the command. This matches debug
’s REPL behavior, making the integration more convenient.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.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.
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.
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.
...
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!