But you can't chain Object#present? together the way Colin is suggesting. Object#present? returns true, not the object. I suggested something like this a while ago, Object#or_if_blank, and it got shot down because it "smelled" -- it's in git://github.com/ryana/rails_ext.git :)
Cheers, Ryan On Sun, Dec 27, 2009 at 8:37 AM, Pratik <[email protected]> wrote: > We already have Object#present? :) > > On Sun, Dec 27, 2009 at 7:49 AM, ColinDKelley <[email protected]> > wrote: > > All, > > > > I'd like to propose the Object#nonblank? in ActiveSupport, layered > > over the blank? method. This has been working well for us in the past > > year and hopefully others will find it useful enough to include in > > core. > > > > It 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' > > > > The ticket is here: > > > > > https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3620-objectnonblank-analogous-to-rubys-numericnonzero#ticket-3620-2 > > > > along with a patch that includes full documentation and tests. > > > > -- > > > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > [email protected]<rubyonrails-core%[email protected]> > . > > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > > > > > > > > > -- > Cheers! > - Pratik > http://m.onkey.org | http://twitter.com/lifo > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<rubyonrails-core%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
