This project is archived and is in readonly mode.
Object#nonblank? (analogous to Ruby's Numeric#nonzero?)
Reported by Colin Kelley | December 27th, 2009 @ 12:29 AM
About a year ago we built on Rails' blank? method with a nonblank? method that is analogous to Ruby's Numeric#nonzero? method: it either returns the object itself (if not blank) or nil (if blank). This makes it easy to treat blank parameters the same as missing ones, and allows chaining.
For example, this:
state = params[:state] unless params[:state].blank? country = params[:country] unless params[:country].blank? region = state || country || 'US'
becomes:
region = params[:state].nonblank? || params[:country].nonblank? || 'US'
This has been working well for us and hopefully others will find it useful to too.
The attached patch has full documentation and tests.
Comments and changes to this ticket
-
Colin Kelley December 27th, 2009 @ 12:34 AM
[oops, reformatting]
For example, this:
state = params[:state] unless params[:state].blank? country = params[:country] unless params[:country].blank? region = state || country || 'US'
becomes:
region = params[:state].nonblank? || params[:country].nonblank? || 'US'
-
Prem Sichanugrist (sikachu) December 27th, 2009 @ 04:36 PM
I have some suggestion. Since we already have
present?
method which would do almost the same thing. Should we modifypresent?
method return object itself if it's notblank?
instead?region = params[:state].present? || params[:country].present? || 'US'
-
DHH December 28th, 2009 @ 01:55 AM
- State changed from new to committed
Committed as Object#presence.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>