I don't think Boolean#presence buys you enough over just a #nil? check to 
warrant the awkwardness of the passed parameter.

On Sunday, March 3, 2019 at 4:54:26 PM UTC-8, Ashwin Kumar Subramanian 
wrote:
>
> From the comments on the file, one of the main short hand usage of 
> 'presence' method is :
>
> # 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'
>
> However the '||' use case does not play well if the Object is a boolean 
> type. Example usecase  :
>
> # checked = false
> # checked.presence || true 
> #=> this returns true even even though checked has a value.
>
> Would it be a good idea to have a default value on presence method the way 
> we can provide one with 'fetch' method ? . Example  :
>
> def presence(default_value = nil)
>   return self if present?
>   default_value
> end
>
> checked = false
> checked.presence(true) #instead of 'checked.presence || value'
> # This also reducers from using '||' and we can just use the argument. 
> Very similar to hash.fetch(key, default_value)
>
> Not a big case/change, but was wondering if someone was having the urge of 
> adding this one line to make life a little easier :)
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to