Project

General

Profile

Feature #12602 ยป add-nil-to-d.patch

Add NilClass#to_d which always returns 0 in BigDecimal - Domon (Chun-wei Kuo), 02/22/2017 11:21 AM

View differences:

ext/bigdecimal/lib/bigdecimal/util.rb
# frozen_string_literal: false
# BigDecimal extends the native NilClass class to provide the #to_d method.
#
# When you require BigDecimal in your application, this method will be available
# on nil.
class NilClass
# call-seq:
# nil.to_d -> 0
#
# Always return 0 in BigDecimal.
#
# require 'bigdecimal'
# require 'bigdecimal/util'
#
# nil.to_d
# # => #<BigDecimal:7ffa299205d8,'0.0',9(27)>
#
def to_d
BigDecimal(0)
end
end
# BigDecimal extends the native Integer class to provide the #to_d method.
#
# When you require the BigDecimal library in your application, this methodwill
test/bigdecimal/test_bigdecimal_util.rb
assert_same(x, x.to_d)
end
def test_NilClass_to_d
assert_equal(BigDecimal(0), nil.to_d)
end
def test_Integer_to_d
assert_equal(BigDecimal(1), 1.to_d)
assert_equal(BigDecimal(2<<100), (2<<100).to_d)
    (1-1/1)