Single-Line if-Statement sans Else
Traditional if-statement in Ruby…
Ternary if-statement (sans else-y colon) raises an exception…
So while you could append : nil
to lower the exception…
I guess it’s better to abandon the ternary and adopt the single-line…
So the lesson I learned is that in addition to the traditional approach, there are both ternary and single-line means of writing if-statements. You could technically use any value instead of the forced : nil
, like : false
, but I chose : nil
because both the traditional and single-line statements return nil
if their conditional evaluates to false
.
Side note: I used the double bang (!!
) because I just learned that little trick, which takes something “truthy” like the integer 1, makes it “not truthy”, aka false
with the first bang, and then makes it explicitly true
with the second bang.