Feature #12602
closedAdd NilClass#to_d
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
        
           Updated by mrkn (Kenta Murata) about 9 years ago
          Updated by mrkn (Kenta Murata) about 9 years ago
          
          
        
        
      
      - Status changed from Open to Assigned
- Assignee set to mrkn (Kenta Murata)
        
           Updated by Domon (Chun-wei Kuo) over 8 years ago
          Updated by Domon (Chun-wei Kuo) over 8 years ago
          
          
        
        
      
      - File add-nil-to-d.patch add-nil-to-d.patch added
Hi, are there any updates or concerns about this feature?
        
           Updated by Domon (Chun-wei Kuo) over 8 years ago
          Updated by Domon (Chun-wei Kuo) over 8 years ago
          
          
        
        
      
      - File deleted (add-nil-to-d.patch)
        
           Updated by wizardofogz (Andy Ogzewalla) about 8 years ago
          Updated by wizardofogz (Andy Ogzewalla) about 8 years ago
          
          
        
        
      
      I think this is a good addition to make behavior of numeric types consistent. Is there a reason this was not done initially?
        
           Updated by isayajin (Isay Sosa) over 7 years ago
          Updated by isayajin (Isay Sosa) over 7 years ago
          
          
        
        
      
      Any updates about this feature? I ended up adding the #to_d by monkey patching NilClass, but I would like this feature to get merged.
        
           Updated by mrkn (Kenta Murata) over 7 years ago
          Updated by mrkn (Kenta Murata) over 7 years ago
          
          
        
        
      
      - Status changed from Assigned to Closed
I've released the new version 1.3.5, that includes nil.to_d.
Please use it.