Skip to content

Comments

Add IRB fix command to rerun with corrected spelling (references ruby/did_you_mean#179)#1174

Open
krsinghshubham wants to merge 2 commits intoruby:masterfrom
krsinghshubham:irb-fix-command
Open

Add IRB fix command to rerun with corrected spelling (references ruby/did_you_mean#179)#1174
krsinghshubham wants to merge 2 commits intoruby:masterfrom
krsinghshubham:irb-fix-command

Conversation

@krsinghshubham
Copy link

@krsinghshubham krsinghshubham commented Feb 18, 2026

Add IRB fix command to rerun with corrected spelling (references ruby/did_you_mean#179)

Summary

Adds a native fix command to IRB that reruns the previous command with corrected spelling when a "Did you mean?" error occurs. This implements the feature requested in ruby/did_you_mean#179.

Example

irb(main):001> ac
# => NameError (undefined local variable or method 'ac' for main)
#    Did you mean?  acc
#    Type `fix` to rerun with the correction.

irb(main):002> fix
# Rerunning with: acc
# => ...
irb(main):001> 1.zeor?
# => NoMethodError (undefined method `zeor?' for 1:Integer)
#    Did you mean?  zero?
#    Type `fix` to rerun with the correction.

irb(main):002> fix
# Rerunning with: 1.zero?
# => true

Why This Change in IRB (Not did_you_mean)?

The Problem

The fix command was originally implemented as an extension in the did_you_mean gem via PR ruby/did_you_mean#204 (https://github.com/ruby/did_you_mean). However, this implementation introduced a load order problem.

  • did_you_mean is a default gem—Ruby loads it at boot, before any application code
  • IRB loads later (e.g., when running rails console or irb)
  • When did_you_mean loads, IRB isn't defined yet, so the extension never loads
  • Result: The fix command didn't work in Rails console or when IRB loads after app initialization

Why IRB Should Own This Feature

  1. It's a REPL feature – The fix command is an IRB command (like exit, load, help). It belongs in the REPL layer.

  2. IRB has the context – IRB already has the eval loop, exception handling, and access to the last executed code. It's the natural place for this logic.

  3. did_you_mean provides the API – The did_you_mean gem adds #corrections to exceptions (NameError, NoMethodError, KeyError, etc.). IRB simply uses this public API—no patching or extension loading required.

  4. No load order issues – When IRB runs, both IRB and did_you_mean are loaded. IRB can check exception.respond_to?(:corrections) and use the fix command natively.

  5. Clean separation of concerns – did_you_mean = spell-checking. IRB = REPL behavior. Each gem does one thing well.

What IRB Gets from did_you_mean

IRB uses only the public API that did_you_mean provides:

  • exception.respond_to?(:corrections) – Check if the exception has suggestions
  • exception.corrections – Get the suggested corrections (e.g., ["zero?"])
  • DidYouMean::Levenshtein.distance – Optional; used for edit-distance filtering when available. If did_you_mean isn't loaded (e.g., --disable-did_you_mean), the fix command still works but skips the distance filter.

No internal APIs, no patching, no extension loading.

Implementation Details

Files Changed

  • lib/irb/command/fix.rb (new) – Fix command and LastError storage
  • lib/irb.rb – Store last correctable exception, show discoverability hint
  • lib/irb/default_commands.rb – Register fix and dym commands

Supported Error Types

NoMethodError, NameError, KeyError, NoMatchingPatternKeyError, LoadError

Correction Logic

  • Single suggestion only (edit distance ≤ 2 when did_you_mean is available)
  • Replacement patterns: identifiers, symbols, strings
  • dym alias for users who use fix as a variable

Discoverability

When a fix is available, IRB shows: Type \fix` to rerun with the correction.`

References

…/did_you_mean#179)

- Add fix command that reruns previous command with Did you mean? correction
- Works with exceptions that have #corrections (from did_you_mean gem)
- Show discoverability hint: Type `fix` to rerun with the correction
- Add dym alias for users who use fix as a variable
- Supports NoMethodError, NameError, KeyError, LoadError, NoMatchingPatternKeyError

Co-authored-by: Cursor <cursoragent@cursor.com>
@krsinghshubham
Copy link
Author

Hi, this PR adds the IRB fix command to rerun with corrected spelling when a "Did you mean?" error occurs (see ruby/did_you_mean#179).

@tompng @st0012 @aycabta I’d appreciate a review when you have time. Thanks.

@hsbt
Copy link
Member

hsbt commented Feb 19, 2026

Please remove PR_DESCRIPTION.md at least. That file is unrelated for this repository.

- Replace dym with retry as backup alias for fix command
- Add public eval_path to Context (replace instance_variable_get)
- Refactor fixable? to use class methods instead of allocate/send
- Use gsub to fix all occurrences (e.g. foo.zeor? && bar.zeor?)
- Clean up NoMatchingPatternKeyError check
- Add MAX_EDIT_DISTANCE, LastError, and fix_apply_correction comments
- Update PR description with all changed files

remove descripiton md

Add fix command tests, extract constants, match hint styling to error message
@krsinghshubham
Copy link
Author

Please remove PR_DESCRIPTION.md at least. That file is unrelated for this repository.

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants