Skip to content

Implement Numeric#coerce for subclasses - #427

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster5
Aug 21, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster5

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

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. Because the fixture used by the
core/range/* specs does undef_method :coerce on a Numeric
subclass, this unblocks six previously unloadable range spec files
(new, cover, include, eql, equal_value, case_compare) plus
core/numeric/coerce_spec (0 → 7) — a full language+core sweep shows
a large gain and no regressions. Whole-suite coverage gate stays at
100.0%.

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit 998d1ef into main Aug 21, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster5 branch August 21, 2026 17:53
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.

1 participant