By convention, functions that return true
or false
have a name which ends with a question mark. The grammar of those function names is odd, though. Compare the following:
Array#empty?
Hash#key?(k)
File#exist?(f)
When read out loud, the full sentence sounds different for each:
Array#empty?
— array is emptyHash#key?(k)
— hash has key k
File#exist?(f)
— file f
does existThe (unofficial) Ruby style guide suggests exist?
over exists?
(extra s
), but Rails only provides exists?
as method names.
An alternative to key?
is has_key?
, which the (unofficial) Ruby style guide discourages. For consistency with the #exist?
, it’d have to be have_key?
which is not used anywhere, as far as I can tell, probably because it sounds weird.
For grammatical consistency, the methods would better be named as follows:
Array#is_empty?
Hash#has_key?(k)
File#exists?(f)
But in these new forms, there’s not much of a reason left to include a trailing ?
. The meaning of these functions is clear without it. Consider:
Array#empty
— used for emptying an arrayArray#is_empty
— used for checking whether an array is emptyCar#key
— used for keying a car (what kind of example is this, Denis?!)Car#keyed
— used for returning a keyed copy of a carCar#is_keyed
— used for checking if the car is keyed.(I don’t own a car, by the way.)