Kernel#present?
present? is a Rails/ActiveSupport method available on all objects. It returns true unless the object is nil or “blank.”
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.
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:
nilfalse- An empty string (
""or'') - An empty array (
[]) or hash ({}) - A string containing only whitespace (unless you include the
whitespaceextension)
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
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 != ""
With Associations
# Check if a user has any posts
if current_user.posts.present?
render @current_user.posts
end
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
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
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"]
See Also
Note: present? requires Rails/ActiveSupport. It is not available in plain Ruby.