
Wyatt G. answered 12/24/20
Software Engineer and Architect Specializing in Ruby on Rails
The code you have written is perfectly fine because it is both correct and easy to understand when read.
There are alternate ways to express the same logic, of course. Ruby has .nil? and .zero? methods so you could do this:
If you really want to "code golf" this, you could use Ruby's safe navigation operator &. (which automatically checks for nil before calling a method) and Ruby's nonzero? method to get this:
And here you can try it out yourself to see that it's correct: