Reputation: 23
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.)
Upvotes: 2
Views: 1823
Reputation: 35360
present?
returns the opposite of blank?
. They're Rails extensions of Ruby's core Object
. .present?
will return false
if the value being testing is nil
, ""
, {}
, false
, []
, etc... based on the type (Array
, Hash
, String
, etc...) extending Object
.
Methods like name?
which are based on the attributes of your model like :name
are actually passed through the method_missing
method in ActiveRecord::AttributeMethods
which is then processed by ActiveModel::AttributeMethods
and eventually processed by this class.
So, yes they are mostly the same (for certain column types blank?
is actually called; the same blank?
in present?
above), but they're not identical, as the ?
suffix methods do some additional checking based on the column type.
Upvotes: 7