Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions activesupport/lib/active_support/core_ext/numeric/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,30 @@ module ActiveSupport::NumericWithFormat
# 1234567.to_s(:human, precision: 1,
# separator: ',',
# significant: false) # => "1,2 Million"
def to_s(*args)
format, options = args
options ||= {}

def to_s(format = nil, options = nil)
case format
when nil
super()
when Integer, String
super(format)
when :phone
return ActiveSupport::NumberHelper.number_to_phone(self, options)
return ActiveSupport::NumberHelper.number_to_phone(self, options || {})
when :currency
return ActiveSupport::NumberHelper.number_to_currency(self, options)
return ActiveSupport::NumberHelper.number_to_currency(self, options || {})
when :percentage
return ActiveSupport::NumberHelper.number_to_percentage(self, options)
return ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
when :delimited
return ActiveSupport::NumberHelper.number_to_delimited(self, options)
return ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
when :rounded
return ActiveSupport::NumberHelper.number_to_rounded(self, options)
return ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
when :human
return ActiveSupport::NumberHelper.number_to_human(self, options)
return ActiveSupport::NumberHelper.number_to_human(self, options || {})
when :human_size
return ActiveSupport::NumberHelper.number_to_human_size(self, options)
return ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
when Symbol
super()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this clause is still wrong - we should either delete it and it'll raise the appropriate error (TypeError from Integer or ArgumentError from Float), or we should raise an ArgumentError with an unknown format message rather than just silently returning the default. That would be a better developer experience since it'd blow up if there was a typo in the format name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.. but IMO we should probably keep that change away from this perf one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I agree as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 performance change can be backported to 5-0-stable but change to raising can't.

else
if is_a?(Float) || format.is_a?(Symbol)
super()
else
super
end
super(format)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/numeric_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ def test_default_to_s

assert_equal "1000010.0", BigDecimal("1000010").to_s
assert_equal "10000 10.0", BigDecimal("1000010").to_s("5F")

assert_raises TypeError do
1.to_s({})
end
end

def test_in_milliseconds
Expand Down