Skip to content

Commit 7dbbe1d

Browse files
committed
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.
1 parent 021c554 commit 7dbbe1d

File tree

1 file changed

+10
-55
lines changed

1 file changed

+10
-55
lines changed

lib/bigdecimal/util.rb

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,13 @@
77

88
require 'bigdecimal'
99

10-
class Integer < Numeric
10+
class Numeric
1111
# call-seq:
12-
# int.to_d -> bigdecimal
12+
# num.to_d -> bigdecimal
13+
# num.to_d(precision) -> bigdecimal
1314
#
14-
# Returns the value of +int+ as a BigDecimal.
15-
#
16-
# require 'bigdecimal'
17-
# require 'bigdecimal/util'
18-
#
19-
# 42.to_d # => 0.42e2
20-
#
21-
# See also Kernel.BigDecimal.
22-
#
23-
def to_d
24-
BigDecimal(self)
25-
end
26-
end
27-
28-
29-
class Float < Numeric
30-
# call-seq:
31-
# float.to_d -> bigdecimal
32-
# float.to_d(precision) -> bigdecimal
15+
# Returns the value as a BigDecimal.
3316
#
34-
# Returns the value of +float+ as a BigDecimal.
3517
# The +precision+ parameter is used to determine the number of
3618
# significant digits for the result. When +precision+ is set to +0+,
3719
# the number of digits to represent the float being converted is determined
@@ -41,18 +23,16 @@ class Float < Numeric
4123
# require 'bigdecimal'
4224
# require 'bigdecimal/util'
4325
#
44-
# 0.5.to_d # => 0.5e0
45-
# 1.234.to_d # => 0.1234e1
46-
# 1.234.to_d(2) # => 0.12e1
26+
# Rational(22, 7).to_d(3) # => 0.314e1
27+
# 3.14.to_d(3) # => 0.314e1
28+
# 3.to_d(3) # => 0.3e1
4729
#
4830
# See also Kernel.BigDecimal.
49-
#
5031
def to_d(precision=0)
5132
BigDecimal(self, precision)
5233
end
5334
end
5435

55-
5636
class String
5737
# call-seq:
5838
# str.to_d -> bigdecimal
@@ -108,32 +88,7 @@ def to_digits
10888
# d.to_d # => 0.314e1
10989
#
11090
def to_d
111-
self
112-
end
113-
end
114-
115-
116-
class Rational < Numeric
117-
# call-seq:
118-
# rat.to_d(precision) -> bigdecimal
119-
#
120-
# Returns the value as a BigDecimal.
121-
#
122-
# The +precision+ parameter is used to determine the number of
123-
# significant digits for the result. When +precision+ is set to +0+,
124-
# the number of digits to represent the float being converted is determined
125-
# automatically.
126-
# The default +precision+ is +0+.
127-
#
128-
# require 'bigdecimal'
129-
# require 'bigdecimal/util'
130-
#
131-
# Rational(22, 7).to_d(3) # => 0.314e1
132-
#
133-
# See also Kernel.BigDecimal.
134-
#
135-
def to_d(precision=0)
136-
BigDecimal(self, precision)
91+
self # override to return the same object
13792
end
13893
end
13994

@@ -180,7 +135,7 @@ class NilClass
180135
#
181136
# nil.to_d # => 0.0
182137
#
183-
def to_d
184-
BigDecimal(0)
138+
def to_d(precision=0)
139+
BigDecimal(0, precision)
185140
end
186141
end

0 commit comments

Comments
 (0)