I've got constructs like the following that Rubocop complains about that I'm trying to appease
unless foo && !foo.empty? ...
(item exists and is not empty)
and
bar && !bar.positive?
(item exists and is not positive)
In both cases, Rubocop throws a Style/SafeNavigation
and says I should use &.
instead. However, there does not seem to be a way to write it.
In both cases part of the problem is that there is no concise way of expressing the opposite function - there is no "not empty" for strings, and no "negative or zero" for numbers. Writing bar&.negative?
returns the opposite result for zero, for example.
In the other case, writing unless !foo&.empty?
has Rubocop complain about unless+a negation, but again there's no way to rewrite it as an if foo&.
with any operation that exists.
(btw this is straight Ruby, no Rails, so I don't have blank?
and present?
)
This is an oversight in RuboCop, because the suggested correction can not be guaranteed to have the same semantics. It will be fixed in the upcoming 0.50.1 patch, and the cop will ignore cases with negation.
In the meanwhile, if you're dealing with conditions where your variable could be
nil
, one option is to use the fact thatnil
responds to the coercion methods. Example, assumingfoo
is either an array ornil
: