Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 28 ’10

nil?, empty? and blank?

In Ruby, you check with nil? if an object is nil:

article = nil
article.nil?  # => true

empty? checks if an element - like a string or an array f.e. - is empty:

# Array
[].empty?   #=> true
# String
"".empty?   #=> true

Rails adds the method blank? to the Object class:

An object is blank if it‘s false, empty, or a whitespace string. For example, “”, ” “, nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?


if !address.blank?

Documentation: nil?, empty? (String, Array) and blank?

24 notes 0 comments


You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus