From 7dbbe1d1a7a44349698d69998f4185d96848d333 Mon Sep 17 00:00:00 2001 From: Felipe Sateler Date: Mon, 8 Sep 2025 18:22:36 -0300 Subject: [PATCH] Implement to_d in Numeric and with the same number of arguments everywhere This allows user code to be more generic, not having to care about the specific class. Preserve the BigDecimal override to allow returning the same object. --- lib/bigdecimal/util.rb | 65 +++++++----------------------------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/lib/bigdecimal/util.rb b/lib/bigdecimal/util.rb index 7c5f32eb..f2a64647 100644 --- a/lib/bigdecimal/util.rb +++ b/lib/bigdecimal/util.rb @@ -7,31 +7,13 @@ require 'bigdecimal' -class Integer < Numeric +class Numeric # call-seq: - # int.to_d -> bigdecimal + # num.to_d -> bigdecimal + # num.to_d(precision) -> bigdecimal # - # Returns the value of +int+ as a BigDecimal. - # - # require 'bigdecimal' - # require 'bigdecimal/util' - # - # 42.to_d # => 0.42e2 - # - # See also Kernel.BigDecimal. - # - def to_d - BigDecimal(self) - end -end - - -class Float < Numeric - # call-seq: - # float.to_d -> bigdecimal - # float.to_d(precision) -> bigdecimal + # Returns the value as a BigDecimal. # - # Returns the value of +float+ as a BigDecimal. # The +precision+ parameter is used to determine the number of # significant digits for the result. When +precision+ is set to +0+, # the number of digits to represent the float being converted is determined @@ -41,18 +23,16 @@ class Float < Numeric # require 'bigdecimal' # require 'bigdecimal/util' # - # 0.5.to_d # => 0.5e0 - # 1.234.to_d # => 0.1234e1 - # 1.234.to_d(2) # => 0.12e1 + # Rational(22, 7).to_d(3) # => 0.314e1 + # 3.14.to_d(3) # => 0.314e1 + # 3.to_d(3) # => 0.3e1 # # See also Kernel.BigDecimal. - # def to_d(precision=0) BigDecimal(self, precision) end end - class String # call-seq: # str.to_d -> bigdecimal @@ -108,32 +88,7 @@ def to_digits # d.to_d # => 0.314e1 # def to_d - self - end -end - - -class Rational < Numeric - # call-seq: - # rat.to_d(precision) -> bigdecimal - # - # Returns the value as a BigDecimal. - # - # The +precision+ parameter is used to determine the number of - # significant digits for the result. When +precision+ is set to +0+, - # the number of digits to represent the float being converted is determined - # automatically. - # The default +precision+ is +0+. - # - # require 'bigdecimal' - # require 'bigdecimal/util' - # - # Rational(22, 7).to_d(3) # => 0.314e1 - # - # See also Kernel.BigDecimal. - # - def to_d(precision=0) - BigDecimal(self, precision) + self # override to return the same object end end @@ -180,7 +135,7 @@ class NilClass # # nil.to_d # => 0.0 # - def to_d - BigDecimal(0) + def to_d(precision=0) + BigDecimal(0, precision) end end