Method: Object#presence

Defined in:
lib/active_support/core_ext/object/blank.rb

#presenceObject

Returns the receiver if it’s present otherwise returns nil. object.presence is equivalent to

object.present? ? object : nil

For example, something like

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

becomes

region = params[:state].presence || params[:country].presence || 'US'

Returns:



45
46
47
# File 'lib/active_support/core_ext/object/blank.rb', line 45

def presence
  self if present?
end