Add ability to nest Money.with_rounding_mode blocks#1128
Merged
yukideluxe merged 11 commits intoRubyMoney:mainfrom Jul 14, 2025
Merged
Add ability to nest Money.with_rounding_mode blocks#1128yukideluxe merged 11 commits intoRubyMoney:mainfrom
Money.with_rounding_mode blocks#1128yukideluxe merged 11 commits intoRubyMoney:mainfrom
Conversation
so that it can emphasize it's behaviour better
So that it won't return to the global value in case if someone will modify down in the stack. For example we might have: ```ruby - initializers/money.rb Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN - controllers/test.rb Money.with_rounding_mode(BigDecimal::ROUND_UP) do object.amount = Money.from_amount(1.234) object.save other_object.amount = object.amount * different_rate end - models/object.rb def save Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do self.tax = object.amount * tax_rate end ... end ``` Above example prior the change would cause other_object to use `ROUND_HALF_EVEN`, despite it's explicitly defined in a `with_rounding_mode` block which sets `ROUND_UP`. The small rounding errors might be tricky to debug, and in worst case scenarios can cumulate to a bigger differences when code operates on multiple amounts that might be incorrectly rounded. Co-Authored-By: Łukasz Wójcik <lukasz.wojcik@simplybusiness.co.uk>
lukasz-wojcik
approved these changes
May 9, 2025
sunny
approved these changes
Jun 26, 2025
Contributor
Author
|
Hey @semmons99. Any chance this could be considered in the next release? |
yukideluxe
approved these changes
Jul 14, 2025
| context "with a block" do | ||
| it "respects the rounding_mode" do | ||
| expect(Money.rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| expect(Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do |
Member
There was a problem hiding this comment.
thanks for changing this to use the non-deprecated method! 😍
| expect(Money.from_amount(2.137).to_d).to eq 2.14 | ||
| end | ||
|
|
||
| it 'safely handles concurrent usage in different threads' do |
Member
There was a problem hiding this comment.
Wow, I always find it very difficult to test these scenarios and you made it so easy. I'll keep this in mind for my future self 👏🏻
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.
WHAT
Allow .with_rounding_mode blocks nesting
WHY
So, that we can safely use the block and not rely on global
rounding_mode.Relates to the comment in PR which introduced the feature #343 (comment)