Project

General

Profile

Actions

Feature #12602

closed

Add NilClass#to_d

Added by Domon (Chun-wei Kuo) almost 8 years ago. Updated almost 6 years ago.

Status:
Closed
Target version:
-
[ruby-core:76472]

Description

bigdecimal/util adds a #to_d method to many native classes, as a shortcut to converting them to BigDecimal, e.g.

require 'bigdecimal'
require 'bigdecimal/util'

1.23.to_d  # => #<BigDecimal:7fa4f11f0958,'0.123E1',18(36)>

However, #to_d is not added to NilClass:

nil.to_d  # => NoMethodError: undefined method `to_d' for nil:NilClass

While to_i, to_f, etc. works on nil:

nil.to_i  # => 0
nil.to_f  # => 0.0

I think it is reasonable to make to_d behave more like its counterparts.

Therefore, I tried implementing the NilClass#to_d method in bigdecimal/util with a test.
The method always returns 0 in BigDecimal.

Please see attached patch.

Thanks! :)


Files

add-nil-to-d.patch (1.34 KB) 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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0