NumberHelper
: handle objects responding to_d
#49576
Merged
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.
Fixes #49572.
Similar problem was also reported here - https://github.com/rails/rails/pull/49466/files#r1349249299.
Regression was introduced in #49466. Before that PR, numbers were checked via
Float()
method https://github.com/rails/rails/pull/49466/files#diff-f242a68844d34525bd9a0e26473578dfb8490bea375bf0c2cbb1a4e7f40cefadL177, which callsto_f
on its arguments (see https://ruby-doc.org/core-3.1.1/Kernel.html#method-i-Float).to_f
is defined onMoney
- https://github.com/RubyMoney/money/blob/ccddfdc9616b3f825578d66a50e718a318221cbc/lib/money/money.rb#L485-L487But after,
BigDecimal()
is used which does not call any of these. So I leftBigDecimal()
for strings only, as these are not fully converted as needed when usingto_f
(https://ruby-doc.org/stdlib-3.1.1/libdoc/bigdecimal/rdoc/String.html#method-i-to_d), only leading characters. And calledto_d
for everything else.to_d
is also defined forMoney
https://github.com/RubyMoney/money/blob/ccddfdc9616b3f825578d66a50e718a318221cbc/lib/money/money.rb#L462-L464.