Last Updated: February 25, 2016
·
785
· peterpengnz

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?

2 Responses
Add your response

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 ·