summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/lib/bigdecimal/util.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bigdecimal/lib/bigdecimal/util.rb')
-rw-r--r--ext/bigdecimal/lib/bigdecimal/util.rb30
1 files changed, 21 insertions, 9 deletions
diff --git a/ext/bigdecimal/lib/bigdecimal/util.rb b/ext/bigdecimal/lib/bigdecimal/util.rb
index 911fa6fe3af..88f490cb45e 100644
--- a/ext/bigdecimal/lib/bigdecimal/util.rb
+++ b/ext/bigdecimal/lib/bigdecimal/util.rb
@@ -5,6 +5,8 @@
# and provides BigDecimal#to_d and BigDecimal#to_digits.
#++
+require 'bigdecimal'
+require 'bigdecimal/util.so'
class Integer < Numeric
# call-seq:
@@ -42,8 +44,8 @@ class Float < Numeric
#
# See also BigDecimal::new.
#
- def to_d(precision=nil)
- BigDecimal(self, precision || Float::DIG)
+ def to_d(precision=Float::DIG)
+ BigDecimal(self, precision)
end
end
@@ -64,13 +66,6 @@ class String
#
# See also BigDecimal::new.
#
- def to_d
- begin
- BigDecimal(self)
- rescue ArgumentError
- BigDecimal(0)
- end
- end
end
@@ -132,3 +127,20 @@ class Rational < Numeric
BigDecimal(self, precision)
end
end
+
+
+class NilClass
+ # call-seq:
+ # nil.to_d -> bigdecimal
+ #
+ # Returns nil represented as a BigDecimal.
+ #
+ # require 'bigdecimal'
+ # require 'bigdecimal/util'
+ #
+ # nil.to_d # => 0.0
+ #
+ def to_d
+ BigDecimal(0)
+ end
+end