From: Wilson Bilkovich Date: 2005-08-11T02:00:50+09:00 Subject: Re: NEWBIE QUESTION: pattern with nil Rails defines a method called "blank?" on the class Object. Here's what it looks like: # "", " ", nil, and 0 are all blank def blank? if respond_to?(:empty?) && respond_to?(:strip) strip.empty? elsif respond_to? :empty? empty? elsif respond_to? :zero? zero? else !self end end On 8/10/05, Austin Ziegler wrote: > On 8/10/05, BearItAll wrote: > > On Wed, 10 Aug 2005 14:44:15 +0900, John Carter wrote: > > > class NilClass > > > def empty? > > > true > > > end > > > end > > > if a.empty? > > I vote for this one because a good programmer is a lazy sod. Where ever > > you would end up doing a thing more than once, write it is such a way that > > you never have to type it again. > > Just, please, don't do it in a library that you unleash on the rest of > the world. It changes the contract for +nil+, and I prefer that it > *not* respond to #empty?, because the concept isn't *quite* accurate. > One may as well define #zero? if you're going to to that (but +nil+ > isn't zero, either, just as it isn't the empty string or array). > Better is to use the foo.nil? or foo.empty? construct and be explicit, > or foo.to_s.empty? if you want to silently cast ("normalize"). > Changing nil to respond to #empty? is an implicit change that also > changes its meaning. >