What is the point of Object#presence in Rails?

Here’s the point:

''.presence
# => nil

so if params[:state] == '':

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

What’s more, it works in similar way (that is, returns nil if object is ’empty’) on every object that responds to empty? method, for example:

[].presence
# => nil

Here’s the documentation, for reference:

http://api.rubyonrails.org/classes/Object.html#method-i-presence

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)