ruby / ruby/irb

`show_source` can't find some sources defined in the session

Open
#974 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Ruby
Stars
478
Forks
158
Avg merge
1d 12h
Merged PRs (30d)
12

Description

Description

irb can find the source of a method defined during the repl session, but not its parent class.

irb(main):001* class Foo
irb(main):002*   def x = "Hello, world!"
irb(main):003> end
=> :x
irb(main):004> Foo.instance_method(:x).source_location
=> ["(irb)", 2]
irb(main):005> show_source Foo#x # Works ✅

From: (irb):2

def x = "Hello, world!"

=> nil
irb(main):006> show_source Foo # Not found ❌
Error: Couldn't locate a definition for Foo
=> nil

A similar thing happens if you execute a script from standard input

$ ruby
class Foo
  def x = "Hello, world!"
end

binding.irb
< I press ctrl-d to send EOF >
irb(main):001> Foo.instance_method(:x).source_location
=> ["-", 2]
irb(main):002> show_source Foo#x # Not found ❌
Error: Couldn't locate a definition for Foo#x # Not found ❌
=> nil
irb(main):003> show_source Foo # Not found ❌
Error: Couldn't locate a definition for Foo # Not found ❌
=> nil
Notes

This does work if running from a saved script:

# example_script.rb
class Foo
  def x = "Hello, world!"
end

binding.irb
ruby ./example_script.rb

From: ./example_script.rb @ line 5 :

    1: class Foo
    2:   def x = "Hello, world!"
    3: end
    4:
 => 5: binding.irb

irb(main):001> show_source Foo

From: ./example_script.rb:1

class Foo
  def x = "Hello, world!"
end

nil

Result of irb_info

irb(main):001> irb_info
Ruby version: 3.3.0
IRB version: irb 1.13.1 (2024-05-05)
InputMethod: RelineInputMethod with Reline 0.5.8
Completion: Autocomplete, RegexpCompletor
RUBY_PLATFORM: x86_64-linux
LANG env: en_US.UTF-8
LC_ALL env: en_US.UTF-8
East Asian Ambiguous Width: 1

## Terminal Emulator

What's your terminal emulator? **iTerm 2.**

## Setting Files

Are you using `~/.irbrc` and `~/.inputrc`? **Nope.**

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the show_source command and compare how it resolves methods and parent classes defined in an IRB session, from standard input, and from a saved script. Reproduce the examples in the issue and make the command locate the session-defined class and method sources consistently without regressing saved-script behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.