lib/bigdecimal/util.rb in bigdecimal-1.3.0 vs lib/bigdecimal/util.rb in bigdecimal-1.3.1

- old
+ new

@@ -1,9 +1,14 @@ # frozen_string_literal: false -# BigDecimal extends the native Integer class to provide the #to_d method. # -# When you require the BigDecimal library in your application, this method will +# bigdecimal/util extends various native classes to provide the #to_d method, +# and provides BigDecimal#to_d and BigDecimal#to_digits. + + +# bigdecimal/util extends the native Integer class to provide the #to_d method. +# +# When you require 'bigdecimal/util' in your application, this method will # be available on Integer objects. class Integer < Numeric # call-seq: # int.to_d -> bigdecimal # @@ -18,13 +23,13 @@ def to_d BigDecimal(self) end end -# BigDecimal extends the native Float class to provide the #to_d method. +# bigdecimal/util extends the native Float class to provide the #to_d method. # -# When you require BigDecimal in your application, this method will be +# When you require 'bigdecimal/util' in your application, this method will be # available on Float objects. class Float < Numeric # call-seq: # flt.to_d -> bigdecimal # @@ -39,13 +44,13 @@ def to_d(precision=nil) BigDecimal(self, precision || Float::DIG) end end -# BigDecimal extends the native String class to provide the #to_d method. +# bigdecimal/util extends the native String class to provide the #to_d method. # -# When you require BigDecimal in your application, this method will be +# When you require 'bigdecimal/util' in your application, this method will be # available on String objects. class String # call-seq: # string.to_d -> bigdecimal # @@ -56,18 +61,22 @@ # # "0.5".to_d # # => 0.5e0 # def to_d - BigDecimal(self) + begin + BigDecimal(self) + rescue ArgumentError + BigDecimal(0) + end end end -# BigDecimal extends the native Numeric class to provide the #to_digits and +# bigdecimal/util extends the BigDecimal class to provide the #to_digits and # #to_d methods. # -# When you require BigDecimal in your application, this method will be +# When you require 'bigdecimal/util' in your application, these methods will be # available on BigDecimal objects. class BigDecimal < Numeric # call-seq: # a.to_digits -> string # @@ -97,12 +106,12 @@ def to_d self end end -# BigDecimal extends the native Rational class to provide the #to_d method. +# bigdecimal/util extends the native Rational class to provide the #to_d method. # -# When you require BigDecimal in your application, this method will be +# When you require 'bigdecimal/util' in your application, this method will be # available on Rational objects. class Rational < Numeric # call-seq: # r.to_d(precision) -> bigdecimal #