In ruby, most methods or keywords that end with ?
return boolean values. And we except them to behave like this. Why does defined?
keyword return somethings else? Or why is there ?
at the end of it?
This question can be understood in two ways:
true
or false
?It's because it encodes more information than simply if something is defined or not:
defined? Class # => "constant"
defined? 42 # => "expression"
defined? nil # => "nil"
defined? x # => nil
?
at the end since as the convention goes, the question mark is reserved for predicates?You are right that this is inconsistent. The most likely reasons are:
Almost always, you will use it as predicate anyway
if defined? x
# do something
end
The shortest alternative, which doesn't sound like a predicate I can think of is definition_type_of
. Generally, you want to keep the reserved words in your language short