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
Open
Add IRB fix command to rerun with corrected spelling (references ruby/did_you_mean#179)#1174krsinghshubham wants to merge 2 commits intoruby:masterfrom
fix command to rerun with corrected spelling (references ruby/did_you_mean#179)#1174krsinghshubham wants to merge 2 commits intoruby:masterfrom
Conversation
…/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>
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).
|
Member
|
Please remove |
- 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
242a956 to
abb4543
Compare
Author
Done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add IRB
fixcommand to rerun with corrected spelling (references ruby/did_you_mean#179)Summary
Adds a native
fixcommand 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
Why This Change in IRB (Not did_you_mean)?
The Problem
The
fixcommand was originally implemented as an extension in thedid_you_meangem via PR ruby/did_you_mean#204 (https://github.com/ruby/did_you_mean). However, this implementation introduced a load order problem.did_you_meanis a default gem—Ruby loads it at boot, before any application coderails consoleorirb)did_you_meanloads, IRB isn't defined yet, so the extension never loadsWhy IRB Should Own This Feature
It's a REPL feature – The fix command is an IRB command (like
exit,load,help). It belongs in the REPL layer.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.
did_you_mean provides the API – The did_you_mean gem adds
#correctionsto exceptions (NameError, NoMethodError, KeyError, etc.). IRB simply uses this public API—no patching or extension loading required.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.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 suggestionsexception.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 storagelib/irb.rb– Store last correctable exception, show discoverability hintlib/irb/default_commands.rb– RegisterfixanddymcommandsSupported Error Types
NoMethodError, NameError, KeyError, NoMatchingPatternKeyError, LoadError
Correction Logic
dymalias for users who usefixas a variableDiscoverability
When a fix is available, IRB shows:
Type \fix` to rerun with the correction.`References