Repository navigation
String#[]: implicit conversions for index, length, and range args - #443
Merged
Merged
Conversation
… args
String#[] (and, by delegation, Symbol#[]) now runs MRI's argument coercions
that the raw slice logic skipped:
- a Float or #to_int object index/length converts to an Integer, truncating
toward zero ("hello"[1.5] => "e", "hello"[1.5, 2.9] => "el");
- a Range argument is accepted when it is a Range subclass or carries
Float/#to_int bounds ("hello"[1.0..3.0] => "ell", "hello"[R.new(1,3)]);
- a lone String argument (the substring form) and the Integer/Regexp forms
are unchanged.
The coercion runs in the strIndexFn wrapper (which has the VM to dispatch
#to_int), leaving the pure stringIndexEnc slice helper untouched. Range
arguments are builtin-unwrapped here because dispatch only unwraps a receiver,
not an argument.
The 1..2 arity is now validated for every form, including the Regexp form,
matching MRI's ArgumentError for 0 or 3+ arguments.
Verified against ruby 4.0.6. Zero-regression rubyspec sweep; +6 on
core/string/element_reference and +5 on core/symbol/element_reference.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
String#[](and, by delegation,Symbol#[]) now applies MRI's argumentconversions that the raw slice logic skipped:
Floator#to_intobject index/length converts to anInteger,truncating toward zero —
"hello"[1.5]→"e","hello"[1.5, 2.9]→"el";Rangeargument is accepted when it is aRangesubclass or carriesFloat/#to_intbounds —"hello"[1.0..3.0]→"ell","hello"[R.new(1,3)];Integer/Regexpforms are unchanged.The coercion lives in the
strIndexFnwrapper (which has the VM to dispatch#to_int), leaving the purestringIndexEncslice helper untouched. ARangeargument is builtin-unwrapped here because dispatch only unwraps a receiver,
not an argument.
The
1..2arity is now validated for every form (includingRegexp), matchingMRI's
ArgumentErrorfor 0 or 3+ arguments.Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep; +6 on
core/string/element_referenceand+5 on
core/symbol/element_reference.🤖 Generated with Claude Code