Repository navigation
Implement Numeric#coerce for subclasses - #427
Merged
Merged
Conversation
Numeric#coerce was defined only on Integer and Float, so a user Numeric subclass did not inherit #coerce: N.new.respond_to?(:coerce) was false (MRI: true), and code relying on the inherited method — for example undef_method :coerce on a Numeric subclass — raised NameError. Numeric#coerce is now defined on Numeric itself (Integer/Float keep their overrides): a same-class argument returns [other, self] unchanged; any other argument pairs both operands converted with Kernel#Float (so #to_f is dispatched, and a non-convertible value raises the same ArgumentError/TypeError Float() would), converting self first to match MRI's error precedence. Verified against ruby 4.0.6. 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.
Numeric#coercewas defined only onIntegerandFloat, so a userNumericsubclass did not inherit#coerce:N.new.respond_to?(:coerce)wasfalse(MRI:true), and code relyingon the inherited method — for example
undef_method :coerceon aNumericsubclass — raisedNameError.Numeric#coerceis now defined onNumericitself (Integer/Floatkeep their overrides): a same-class argument returns
[other, self]unchanged; any other argument pairs both operands converted with
Kernel#Float(so#to_fis dispatched, and a non-convertible valueraises the same
ArgumentError/TypeErrorFloat()would), convertingselffirst to match MRI's error precedence.Verified against ruby 4.0.6. Because the fixture used by the
core/range/*specs doesundef_method :coerceon aNumericsubclass, this unblocks six previously unloadable range spec files
(
new,cover,include,eql,equal_value,case_compare) pluscore/numeric/coerce_spec(0 → 7) — a full language+core sweep showsa large gain and no regressions. Whole-suite coverage gate stays at
100.0%.
🤖 Generated with Claude Code