From: Todd Benson Date: 2007-11-07T11:25:11+09:00 Subject: Re: Is there a "||" that treats "" also as false? On 11/6/07, Todd Benson wrote: > On 11/6/07, yermej wrote: > > On Nov 6, 6:41 pm, Joshua Muheim wrote: > > > Hi all > > > > > > irb(main):001:0> "" || "asdf" > > > => "" > > > irb(main):002:0> nil || "asdf" > > > => "asdf" > > > irb(main):003:0> > > > > > > I'd like the first one to also return "asdf". So is there an operator > > > that fits my needs? :-) > > > > "" isn't false so || and or won't work. You'll have to code it > > differently. E.g.: > > > > a = "a string" > > a || (a.empty? ? "asdf" : a) > > This doesn't work for me. The previous one doesn't either. This one > does (I'm sure someone could easily clean this up, I feel lazy > though)... > > [nil, "", "something"].each do |i| > puts( (item ||= "").empty? ? "asdf" : item ) There should be an additional closing ) on the previous line of code > end Todd