I’m learning Rails and I’m working on an extremely basic RSVP app where if the user has entered both a name and an e-mail address, a method called rsvp? in my User model returns true.
If my method is defined as:
def rsvp?
name.present? && email.present?
end
I get the same results as if it was:
def rsvp?
name? && email?
end
So is saying name.present? the same as name? I was told that name.present? would not only check for nil values, but also for empty string values. But it appears that name? accomplishes the same thing (tested all four permutations of name & e-mail, name but no e-mail, etc.)
present?returns the opposite ofblank?. They’re Rails extensions of Ruby’s coreObject..present?will returnfalseif the value being testing isnil,"",{},false,[], etc… based on the type (Array,Hash,String, etc…) extendingObject.Methods like
name?which are based on the attributes of your model like:nameare actually passed through themethod_missingmethod inActiveRecord::AttributeMethodswhich is then processed byActiveModel::AttributeMethodsand eventually processed by this class.So, yes they are mostly the same (for certain column types
blank?is actually called; the sameblank?inpresent?above), but they’re not identical, as the?suffix methods do some additional checking based on the column type.