ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Using the safe navigation operator with other operators?

Open arempe93 opened this issue 7 years ago • 2 comments

Recently, I came across this:

do_something if value1 &.!= value2

Which was equal parts very cool and startling.

So I wanted to get community feedback on whether or not it was too obscure to be good style. We can add our findings to the style guide, since there is no guidance on it yet

arempe93 avatar Feb 16 '18 20:02 arempe93

My feeling is that these should be highly discouraged for use with comparison operators.

do_something if value1 &.!= value2
# not the same as
do_something unless value1 &.== value2

I personally can't grok either of the above and I have to "unroll" those in my head.

For other operators I am neutral:

ary &.<< elem
foo &.+ bar

marcandre avatar Jun 13 '20 22:06 marcandre

They all seem somewhat confusing to me, especially the comparison ops. Generally, I find it weird you'd care about a nil check in a comparison anyways.

bbatsov avatar Jun 14 '20 09:06 bbatsov