1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allocation free Integer#to_s

This commit is contained in:
Jean Boussier 2017-01-19 11:28:26 +01:00
parent 190b82aee8
commit b9bda7fd89
2 changed files with 19 additions and 16 deletions

View file

@ -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)
else
if is_a?(Float) || format.is_a?(Symbol)
return ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
when Symbol
super()
else
super
end
super(format)
end
end
end

View file

@ -394,6 +394,10 @@ class NumericExtFormattingTest < ActiveSupport::TestCase
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