From: Rob Biedenharn Date: 2007-11-08T02:52:11+09:00 Subject: Re: Is there a "||" that treats "" also as false? On Nov 7, 2007, at 12:36 PM, Joshua Muheim wrote: > I finally came up with this: > > class String > def |(alternative) > (self.nil? or self == "") ? alternative : self > end > end > > class NilClass > def |(alternative) > alternative > end > end > > Is this OK? Or do I accidently override some existing method? Because > without the definition above > > nil | "asdf" > > returns > > true! That's because NilClass#| is already defined so you probably should leave it alone. For the String part, you'll only ever get truth from self.nil? if self really is nil so that method would be better refactored as: class String def |(alternative) empty? ? alternative : self end end -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com