nil.to_d returns 0.0, so Rubocop should not complain about this case in particular.
It complains:
# Lint/SafeNavigationChain: Do not chain ordinary method call after safe navigation operator.
Note: Rubocop doesn't complain about the same code with to_f.
Just write the following code and run Rubocop:
amount&.slice.to_d
0.62.0 (using Parser 2.5.3.0, running on ruby 2.6.0 x86_64-darwin18)
This cop is aware of to_f because nil.methods includes :to_f, but to_d is not included.
https://github.com/rubocop-hq/rubocop/blob/b4f89dc1f42a4c5fbe87b9cde22fd75eff2faf00/lib/rubocop/cop/mixin/nil_methods.rb#L10-L12
to_d is defined by bigdecimal/uti library, so it is not defined in the pure environment such as rubocop process.
nil.to_d rescue p $! # => #<NoMethodError: undefined method `to_d' for nil:NilClass>
require 'bigdecimal'
require 'bigdecimal/util'
p nil.to_d # => 0.0
I think this cop should not complain about to_d. Because it is a standard library method.
By the way, if you'd like to use to_d without any offenses soon, you can configure the cop with Whitelist.
Lint/SafeNavigationChain:
Whitelist:
- to_d
Most helpful comment
This cop is aware of
to_fbecausenil.methodsincludes:to_f, butto_dis not included.https://github.com/rubocop-hq/rubocop/blob/b4f89dc1f42a4c5fbe87b9cde22fd75eff2faf00/lib/rubocop/cop/mixin/nil_methods.rb#L10-L12
to_dis defined bybigdecimal/utilibrary, so it is not defined in the pure environment such as rubocop process.I think this cop should not complain about
to_d. Because it is a standard library method.By the way, if you'd like to use
to_dwithout any offenses soon, you can configure the cop withWhitelist.