提交 7e434d6d 编写于 作者: S Sean Griffin

Merge pull request #20038 from imanel/numeric_prepend

Use Module.prepend instead of alias_method and unify behavior of all Numeric extensions
require 'bigdecimal'
require 'bigdecimal/util'
class BigDecimal
DEFAULT_STRING_FORMAT = 'F'
alias_method :to_default_s, :to_s
module ActiveSupport
module BigDecimalWithDefaultFormat #:nodoc:
DEFAULT_STRING_FORMAT = 'F'
def to_s(format = nil, options = nil)
if format.is_a?(Symbol)
to_formatted_s(format, options || {})
else
to_default_s(format || DEFAULT_STRING_FORMAT)
def to_s(format = nil)
super(format || DEFAULT_STRING_FORMAT)
end
end
end
BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat)
require 'active_support/core_ext/big_decimal/conversions'
require 'active_support/number_helper'
class Numeric
module ActiveSupport::NumericWithFormat
# Provides options for converting numbers into formatted strings.
# Options are provided for phone numbers, currency, percentage,
......@@ -97,7 +97,10 @@ class Numeric
# 1234567.to_s(:human, precision: 1,
# separator: ',',
# significant: false) # => "1,2 Million"
def to_formatted_s(format = :default, options = {})
def to_s(*args)
format, options = args
options ||= {}
case format
when :phone
return ActiveSupport::NumberHelper.number_to_phone(self, options)
......@@ -114,32 +117,16 @@ def to_formatted_s(format = :default, options = {})
when :human_size
return ActiveSupport::NumberHelper.number_to_human_size(self, options)
else
self.to_default_s
end
end
[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
super
end
end
Float.class_eval do
alias_method :to_default_s, :to_s
def to_s(*args)
if args.empty?
to_default_s
else
to_formatted_s(*args)
end
end
def to_formatted_s(*args)
to_s(*args)
end
deprecate to_formatted_s: :to_s
end
[Fixnum, Bignum, Float, BigDecimal].each do |klass|
klass.prepend(ActiveSupport::NumericWithFormat)
end
......@@ -2073,30 +2073,22 @@ Extensions to `BigDecimal`
--------------------------
### `to_s`
The method `to_s` is aliased to `to_formatted_s`. This provides a convenient way to display a BigDecimal value in floating-point notation:
The method `to_s` provides a default specifier of "F". This means that a simple call to `to_s` will result in floating point representation instead of engineering notation:
```ruby
BigDecimal.new(5.00, 6).to_s # => "5.0"
```
### `to_formatted_s`
Te method `to_formatted_s` provides a default specifier of "F". This means that a simple call to `to_formatted_s` or `to_s` will result in floating point representation instead of engineering notation:
```ruby
BigDecimal.new(5.00, 6).to_formatted_s # => "5.0"
```
and that symbol specifiers are also supported:
```ruby
BigDecimal.new(5.00, 6).to_formatted_s(:db) # => "5.0"
BigDecimal.new(5.00, 6).to_s(:db) # => "5.0"
```
Engineering notation is still supported:
```ruby
BigDecimal.new(5.00, 6).to_formatted_s("e") # => "0.5E1"
BigDecimal.new(5.00, 6).to_s("e") # => "0.5E1"
```
Extensions to `Enumerable`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册