From: Brian Candler Date: 2003-07-17T23:27:26+09:00 Subject: Re: nil_or_empty? on String 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?) But more pragmatically, I think I would write: if pswd.to_s != "" and user.to_s == "" since I guess sooner or later, 'user' and 'pswd' are going to have to be treated as a string anyway. Alternatively, since your code treats "empty string" and "nil" as the same thing, you could actually convert them to the same thing so you don't need to treat them as separate cases: user = nil if user == "" pswd = nil if pswd == "" ... if pswd and not user raise "Cannot specify password without username" end Cheers, Brian.