Delegate Money#clamp to Comparable#clamp - #481
Open
seanpdoyle wants to merge 1 commit into
Open
seanpdoyle wants to merge 1 commit into
seanpdoyle wants to merge 1 commit into
Conversation
seanpdoyle
force-pushed
the
numeric-clamp
branch
from
December 1, 2025 21:19
84f60c0 to
e5d5aec
Compare
seanpdoyle
force-pushed
the
numeric-clamp
branch
from
December 1, 2025 21:33
e5d5aec to
7c1ebdc
Compare
The [Comparable#clamp][] interface supports two additional styles of invocation that `Money#clamp` does not: * a [Range][] instance * a `nil` maximum To support those invocations, this commit uses the `...` argument forwarding syntax to pass along any arguments to the underlying `#clamp` implementation. To ensure that the `ArgumentError` is still raised, this commit introduces test coverage. The prior error's message (`min cannot be greater than max`) is replaced with the message provided by the standard library (`min argument must be less than or equal to max argument`). [Comparable#clamp]: https://docs.ruby-lang.org/en/master/Comparable.html#method-i-clamp [Range]: https://docs.ruby-lang.org/en/master/Range.html
seanpdoyle
force-pushed
the
numeric-clamp
branch
from
December 1, 2025 21:38
7c1ebdc to
e253458
Compare
There was a problem hiding this comment.
Pull request overview
Delegates Money#clamp to Ruby’s Comparable#clamp, adding support for ranges and unbounded maxima.
Changes:
- Forward clamp arguments to the underlying value.
- Add range,
nilmaximum, and error tests. - Update clamp examples.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/money/money.rb |
Implements delegation and updates documentation. |
spec/money_spec.rb |
Covers newly supported invocation styles. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| # @example | ||
| # Money.new(50, "CAD").clamp(1, 100) #=> Money.new(50, "CAD") | ||
| # Money.new(50, "CAD").clamp(1..100) #=> Money.new(50, "CAD") | ||
| # Money.new(50, "CAD").clamp(1, nil) #=> Money.new(1, "CAD") |
|
|
||
| it 'raises an Argument error if the max value is less than the min value' do | ||
| money = Money.new(-9001, 'EUR').clamp(min, nil) | ||
| expect { money.clamp(max, min) }.to raise_error(ArgumentError, 'min argument must be less than or equal to max argument') |
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.
The Comparable#clamp interface supports two additional styles of invocation that
Money#clampdoes not:nilmaximumTo support those invocations, this commit uses the
...argument forwarding syntax to pass along any arguments to the underlying#clampimplementation.To ensure that the
ArgumentErroris still raised, this commit introduces test coverage. The prior error's message (min cannot be greater than max) is replaced with the message provided by the standard library (min argument must be less than or equal to max argument).