Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion proposing Object#nonblank? (analogous to Ruby's Numeric#nonzero?)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ColinDKelley  
View profile  
 More options Dec 27 2009, 11:58 am
From: ColinDKelley <colindkel...@gmail.com>
Date: Sun, 27 Dec 2009 08:58:25 -0800 (PST)
Local: Sun, Dec 27 2009 11:58 am
Subject: Re: proposing Object#nonblank? (analogous to Ruby's Numeric#nonzero?)

> We already have Object#present? :)

True, but present? is just the inverse of blank?.  What we've found
very useful is to have a method that treats blank parameters the same
as missing ones, and allows chaining.

Check out the example:

  state   = params[:state]   unless params[:state].blank?
  country = params[:country] unless params[:country].blank?
  region  = state || country || 'US'

Rewriting with present? isn't any more expressive:

  state    = params[:state]     if params[:state].present?
  country = params[:country] if params[:country].present?
  region   = state || country || 'US'

But nonblank? makes it more concise and expressive IMO:

  region = params[:state].nonblank? || params[:country].nonblank? ||
'US'

We've been using it this way for the last year and it really has
cleaned up a lot of code.  More importantly it has become a reflex to
use it inline--as shown above--that has helped us avoid bugs where we
might think a parameter was present but really it was just there with
an empty value.

I suppose we could keep the method name present? but switch its
behavior to match what's proposed here for nonblank?.  But that
contract change could break someone's code who was depending on a
boolean being returned.  Also I prefer that nonblank? has a name that
parallels nonzero? from Ruby.

I like the symmetrical pair of nonzero? and nonblank? because they map
values (0, empty/blank string respectively) to nil that are typically
equivalent to not being present at all.  Other languages like Python
found it convenient to have 0 and empty string treated as false for
just this reason I think.

-Colin

On Dec 27, 5:37 am, Pratik <pratikn...@gmail.com> wrote:

> We already have Object#present? :)

> On Sun, Dec 27, 2009 at 7:49 AM, ColinDKelley <colindkel...@gmail.com> 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/3...

> > 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 rubyonrails-core@googlegroups.com.
> > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-core?hl=en.

> --
> Cheers!
> - Pratikhttp://m.onkey.org|http://twitter.com/lifo


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.