Reason #11 • January 11th, 2026

IRB: show_source and edit commands

While FAFO'ing in irb, an invaluable tool is the show_source command. It allows printing the source location and source code of methods directly inside the REPL:

Ruby
show_source JSON.parse

# Output:
# From: .../ruby/3.4.7/lib/ruby/gems/3.4.0/gems/json-2.18.0/lib/json/common.rb:351
#
#  def parse(source, opts = nil)
#    opts = ParserOptions.prepare(opts) unless opts.nil?
#    Parser.parse(source, opts)
#  end
    

This command is also aliased as $ for even quicker access:

Ruby
$ JSON.load_file

# Output:
# From: .../ruby/3.4.7/lib/ruby/gems/3.4.0/gems/json-2.18.0/lib/json/common.rb:388
#
#  def load_file(filespec, opts = nil)
#    parse(File.read(filespec, encoding: Encoding::UTF_8), opts)
#  end
    

This is particularly useful for quickly inspecting methods from libraries or gems without having to leave the irb session. In case you want to dig deeper, you can copy the file path and open the file in your editor of choice.

Just kidding, why go through all that trouble when you can open your favourite editor from within irb itself?

Ruby
edit JSON.load_file

If your mind is 🤯 at this point, go ahead and share this TIL with friends and family at your leisure.

Limitations

You can only use show_source and edit on methods that have Ruby source code available. This means that you cannot use them on methods implemented in C or other languages, or on methods dynamically generated via meta programming.

Commands in IRB

While these invocations can be mistaken for regular Ruby methods, they are not. It would not be possible using pure Ruby to pass a method like JSON.load_file as an argument to another method, as this would just try to call the method and pass its return value.

If you try to invoke them as methods using parentheses, you encounter an error like this:

Ruby
JSON.load_file("JSON.load_file")
# => in 'load': wrong number of arguments (given 0, expected 1..3) (ArgumentError)
    

If you wish to explore commands further, you can use the help command to get a list of all commands built in to irb. It is also possible to extend irb with custom commands as explained in the IRB documentation. Though I have never needed to do so myself.

Reason #12 ?