rubyguides

Kernel#present?

present? is a Rails/ActiveSupport method available on all objects. It returns true unless the object is nil or blank.

In everyday Rails code, present? is mostly a readability helper. It lets you write the intent directly, instead of spelling out multiple nil and empty checks every time you validate input or decide whether to render something.

Description

The present? method is part of ActiveSupport’s Core Extensions, which add useful methods to Ruby’s built-in objects. Unlike Ruby’s native nil?, which only checks for nil, present? also detects blank values like empty strings, empty arrays, and empty hashes.

This method is the opposite of blank?. If object.blank? returns true, then object.present? returns false, and vice versa.

That relationship is useful because it gives you a pair of predicates that describe opposite sides of the same condition. When the code is checking whether something exists, present? usually reads more naturally than a chain of low-level comparisons.

Signature

object.present?

Return Value

Returns true if the object is not nil and not blank. Returns false otherwise.

What is “blank”?

An object is considered blank if it is any of the following:

  • nil
  • false
  • An empty string ("" or '')
  • An empty array ([]) or hash ({})
  • A string containing only whitespace (unless you include the whitespace extension)

Parameters

This method takes no parameters.

Examples

Basic Usage

"hello".present?      # => true
"".present?          # => false
nil.present?         # => false
[1, 2, 3].present?   # => true
[].present?          # => false
{}.present?          # => false

These examples show the main rule: values with content are present, and empty or nil values are not. That makes the method easy to scan in controllers, helpers, and views where the presence check is only one line of a larger workflow.

In Conditionals

present? is commonly used in Rails views and controllers:

if params[:search].present?
  @results = SearchService.call(params[:search])
end

# More concise than:
# if params[:search] && params[:search].strip != ""

That pattern is one of the most common reasons people reach for present?. The code stays compact, and the caller does not need to remember the exact combination of nil checks and whitespace trimming.

It also keeps the condition readable when the surrounding method already has other branches. A single presence check can be easier to scan than several low-level comparisons spread across the line.

With Associations

# Check if a user has any posts
if current_user.posts.present?
  render @current_user.posts
end

The association example is a nice reminder that present? is not just for strings. It works on collections too, which makes it handy when deciding whether there is anything worth displaying.

That broader use is what makes the helper feel natural in Rails. Whether the object is a string, an association, or an array, the same predicate can answer the question cleanly.

Comparison with ruby native methods

# Ruby native - only checks nil
"hello".nil?    # => false
nil.nil?        # => true
"".nil?         # => false (empty string is NOT nil)

# Rails present? - checks nil AND blank
"hello".present?    # => true
nil.present?        # => false
"".present?         # => false

Comparing the Rails helper with nil? shows why it exists. nil? only checks for nil, while present? gives you a more practical answer for user-facing code that also needs to treat empty values as absent.

Common Patterns

Safe Navigation

Use the safe navigation operator (&.) when you’re unsure if an object might be nil:

current_user&.name&.present?
# This won't raise NoMethodError if current_user is nil

Safe navigation pairs well with present? because the method itself is already a yes-or-no check. The combination keeps the code compact while still protecting you from nil receivers.

Once you use them together a few times, the pattern becomes easy to read. It says, in order, “only if the object exists, then check whether it has meaningful content.”

Combining with other methods

# reject blank values from an array
[1, "", nil, 2, "  "].reject(&:blank?)
# => [1, 2]

# select only present values
[nil, "a", "", "b"].select(&:present?)
# => ["a", "b"]

Using present? with other enumerable methods keeps the code expressive. You can filter a list down to the values that matter without having to write a custom predicate for every call site.

That is especially helpful when you are cleaning up mixed user input. The method gives you one simple boolean, and the enumerable methods do the rest of the work.

See Also

  • nil? — Checks if an object is nil
  • empty? — Checks if a collection is empty

Note: present? requires Rails/ActiveSupport. It is not available in plain Ruby.