diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-11-29 05:07:40 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-11-29 05:07:40 -0200 |
commit | 4eb68d8ed47c383f717efc8ad757949596a8b99c (patch) | |
tree | de5c551da6cee48a3522160594de15fa90f9209e /activesupport/lib | |
parent | 86857868f8bdb33a865d9483c53cd0a5583bb7cc (diff) | |
parent | e3cba67824499804065f385b9e6cc1f73596f5ee (diff) | |
download | rails-4eb68d8ed47c383f717efc8ad757949596a8b99c.tar.gz rails-4eb68d8ed47c383f717efc8ad757949596a8b99c.tar.bz2 rails-4eb68d8ed47c383f717efc8ad757949596a8b99c.zip |
Merge pull request #17816 from byroot/prevent-numeric-to-s-to-allocate-an-array
Prevent Numeric#to_s from allocating an array
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/big_decimal/conversions.rb | 13 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/numeric/conversions.rb | 28 |
2 files changed, 25 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 843c592669..234283e792 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -3,14 +3,13 @@ require 'bigdecimal/util' class BigDecimal DEFAULT_STRING_FORMAT = 'F' - def to_formatted_s(*args) - if args[0].is_a?(Symbol) - super + alias_method :to_default_s, :to_s + + def to_s(format = nil, options = nil) + if format.is_a?(Symbol) + to_formatted_s(format, options || {}) else - format = args[0] || DEFAULT_STRING_FORMAT - _original_to_s(format) + to_default_s(format || DEFAULT_STRING_FORMAT) end end - alias_method :_original_to_s, :to_s - alias_method :to_s, :to_formatted_s end diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb index 6d3635c69a..0c8ff79237 100644 --- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb +++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb @@ -118,18 +118,28 @@ class Numeric end end - [Float, Fixnum, Bignum, BigDecimal].each do |klass| - klass.send(:alias_method, :to_default_s, :to_s) - - klass.send(:define_method, :to_s) do |*args| - if args[0].is_a?(Symbol) - format = args[0] - options = args[1] || {} + [Fixnum, Bignum].each do |klass| + klass.class_eval do + alias_method :to_default_s, :to_s + def to_s(base_or_format = 10, options = nil) + if base_or_format.is_a?(Symbol) + to_formatted_s(base_or_format, options || {}) + else + to_default_s(base_or_format) + end + end + end + end - self.to_formatted_s(format, options) + Float.class_eval do + alias_method :to_default_s, :to_s + def to_s(*args) + if args.empty? + to_default_s else - to_default_s(*args) + to_formatted_s(*args) end end end + end |