This is basically one of the core ideas of Ruby: that you can read it like a story. Once you are used to reading it as “do X unless Y”, you will miss it in other languages. Note that I wrote Y, not Y is true. Because often Y is a statement that has meaning in itself.
Example:
# imagine that given_name is some text from user input# this is of course valid:
user.name = given_name if !user.is_locked
# but this reads more fluently:
user.name = given_name unless user.is_locked
Ruby also allows using ? as last character in method names, which is a convention for methods that return either true or false.
Same goes for ! (the bang operator), that is commonly used to tell developers that there exists a non-bang version of the same method as well. The bang method is often the more strict version (e.g. raises an error instead of returning nil; or having side effects compared to the non-bang version).
So the above example may be more commonly written like this:
user.name = given_name unless user.locked?
and the question mark makes you automatically adding is or has while reading: Set the user’s name to the given_name unless the user is locked
Of course this is all by convention and you may also do it different. But that’s how most Ruby code is written.
To stay consistent, lots of projects use RuboCop, which warns you on inconsistency based on your project’s settings.
That’s way too many ways of doing the same thing, yuck.
But you’re saying the idiomatic way is to use
unless
, rather than the actual operator for this?This is basically one of the core ideas of Ruby: that you can read it like a story. Once you are used to reading it as “do X unless Y”, you will miss it in other languages. Note that I wrote Y, not Y is true. Because often Y is a statement that has meaning in itself.
Example:
# imagine that given_name is some text from user input # this is of course valid: user.name = given_name if !user.is_locked # but this reads more fluently: user.name = given_name unless user.is_locked
Ruby also allows using
?
as last character in method names, which is a convention for methods that return eithertrue
orfalse
.Same goes for
!
(the bang operator), that is commonly used to tell developers that there exists a non-bang version of the same method as well. The bang method is often the more strict version (e.g. raises an error instead of returningnil
; or having side effects compared to the non-bang version).So the above example may be more commonly written like this:
user.name = given_name unless user.locked?
and the question mark makes you automatically adding is or has while reading: Set the user’s name to the given_name unless the user is locked
Of course this is all by convention and you may also do it different. But that’s how most Ruby code is written.
To stay consistent, lots of projects use RuboCop, which warns you on inconsistency based on your project’s settings.
I honestly don’t like the “unless X” paradigm, because it adds the condition at the end. The
??=
operator shows us what will happen much sooner.I guess it’s a matter of personal taste.