Don't forget present? method
In Rails, we use blank?
method a lot and I think it is handy.
From the documentation it says:
blank?
An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are all blank.
However, if your code looks like:
unless something.blank?
# then do some action here
end
do you know there is another method called present?
present?
An object is present if it’s not blank?.
Now we could have:
if something.present?
# then do some action here
end
Is it a lot nicer?
Written by Peter Zhang
Related protips
2 Responses
Yes
over 1 year ago
·
wowwww, din't pay attention to that ever, can you just tell me the difference between nil?, blank?, present?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Related Tags
#rails
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#