From: Martin DeMello Date: 2003-07-18T00:17:03+09:00 Subject: Re: nil_or_empty? on String Brian Candler wrote: > On Thu, Jul 17, 2003 at 11:16:45PM +0900, Chris Morris wrote: >> I just threw down the following -- it smells like I'm over-complicating >> matters here. Is there a more elegant to do this? > .. >> I wanted to avoid this: >> >> if (user.nil? || user.empty?) && !(pswd.nil? || pswd.empty?) >> >> ...which I guess isn't that bad now that I read it, although >> nil_or_empty? will come in handy if I need to re-use it a lot. > > We can assume that 'false' is also an invalid value for username/ password - > since false.empty? raises an exception - in which case, > > if pswd && !pswd.empty? && (!user || user.empty?) if [user, pswd].any? {|i| i.nil? || i.empty?} or, if you want to be terse at the expense of readability unless [user, pswd].any? {|i| i && i[0]} or even if [user, pswd].compact.mapf(:empty?).any? where mapf is defined in http://www.rubygarden.org/ruby?AutoMap martin