Skip to content

Commit

Permalink
add NilClass#to_d
Browse files Browse the repository at this point in the history
  • Loading branch information
joseramonc committed Apr 9, 2018
1 parent d8e1446 commit c033c71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/bigdecimal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,20 @@ def to_d(precision)
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
4 changes: 4 additions & 0 deletions test/test_bigdecimal_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ def test_invalid_String_to_d
assert_equal("invalid".to_d, BigDecimal('0.0'))
end

def test_Nil_to_d
assert_equal(nil.to_d, BigDecimal('0.0'))
end

end

0 comments on commit c033c71

Please sign in to comment.