diff options
author | Andrew White <pixeltrix@users.noreply.github.com> | 2017-01-19 15:24:48 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-19 15:24:48 +0000 |
commit | 9a1f4bcf9c7bcb1fb19af9668d168d0356caab9a (patch) | |
tree | ac8c88101c16229b70ebfa570fcbe8457a21f7b3 /activesupport | |
parent | 893d5fb0a24bc69b07574924d78ca7f9826f9403 (diff) | |
parent | b9bda7fd891147a0bc0cffa6dd9d15601be8b472 (diff) | |
download | rails-9a1f4bcf9c7bcb1fb19af9668d168d0356caab9a.tar.gz rails-9a1f4bcf9c7bcb1fb19af9668d168d0356caab9a.tar.bz2 rails-9a1f4bcf9c7bcb1fb19af9668d168d0356caab9a.zip |
Merge pull request #27736 from Shopify/reduce-numeric-with-format-allocations
Allocation free Integer#to_s
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/numeric/conversions.rb | 31 | ||||
-rw-r--r-- | activesupport/test/core_ext/numeric_ext_test.rb | 4 |
2 files changed, 19 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb index 946f8ddeab..4f6621693e 100644 --- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb +++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb @@ -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() else - if is_a?(Float) || format.is_a?(Symbol) - super() - else - super - end + super(format) end end end diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 5f86bf97c8..3cfbe6e7e6 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -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 |