This project is archived and is in readonly mode.

#3620 ✓committed
Colin Kelley

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

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>

Pages