instance method
presence
Ruby on Rails edge
Since v2.3.18Signature
presence()
Returns the receiver if it’s present otherwise returns nil.
`object.presence` is equivalent to
```ruby
object.present? ? object : nil
```
For example, something like
```ruby
state = params[:state] if params[:state].present?
country = params[:country] if params[:country].present?
region = state || country || 'US'
```
becomes
```ruby
region = params[:state].presence || params[:country].presence || 'US'
```
@return [Object]
: () -> self?
Source
# File activesupport/lib/active_support/core_ext/object/blank.rb, line 58
def presence
self if present?
end
Defined in activesupport/lib/active_support/core_ext/object/blank.rb line 58
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Object