aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/numeric/conversions.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2014-11-28 13:01:03 -0500
committerJean Boussier <jean.boussier@gmail.com>2014-11-28 15:25:15 -0500
commite3cba67824499804065f385b9e6cc1f73596f5ee (patch)
treeaff8a210b58d2360548f23237de99027be8dc311 /activesupport/lib/active_support/core_ext/numeric/conversions.rb
parentee614af6fa6c9e8cac70bcfd7d3583d0b4ff907b (diff)
downloadrails-e3cba67824499804065f385b9e6cc1f73596f5ee.tar.gz
rails-e3cba67824499804065f385b9e6cc1f73596f5ee.tar.bz2
rails-e3cba67824499804065f385b9e6cc1f73596f5ee.zip
Prevent Numeric#to_s from allocating an array
Diffstat (limited to 'activesupport/lib/active_support/core_ext/numeric/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/numeric/conversions.rb28
1 files changed, 19 insertions, 9 deletions
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