I just threw down the following – it smells like I’m over-complicating
matters here. Is there a more elegant to do this?
if (user.nil_or_empty?) && (!pswd.nil_or_empty?)
raise 'cannot specify pswd without specifying user'
end
class String
def nil_or_empty?
empty?
end
end
class NilClass
def nil_or_empty?
nil?
end
end
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.
···
–
Chris
http://clabs.org/blogki
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Free solo piano album (mp3)
http://cministries.org/cstudios/blackandwhite.htm
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.
···
On Thu, Jul 17, 2003 at 11:16:45PM +0900, Chris Morris wrote:
Just a nit … shouldn’t this …
class NilClass
def nil_or_empty?
nil?
end
end
be this instead? …
class NilClass
def nil_or_empty?
true
end
end
···
On Thu, 2003-07-17 at 10:16, Chris Morris wrote:
“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)
I wanted to avoid this:
if (user.nil? || user.empty?) && !(pswd.nil? || pswd.empty?)
You could do something like:
if user.to_s.empty? && pswd.to_s.empty?
Jason Creighton
But more pragmatically, I think I would write:
if pswd.to_s != "" and user.to_s == ""
I think I will, too. My nose was correct. Thx for the virtual pairing
Chris
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
···
Brian Candler B.Candler@pobox.com 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?)