From: Joel VanderWerf Date: 2007-04-16T04:22:38+09:00 Subject: Re: Idiom wanted (now hiring!) > On Apr 13, 1:02 am, Jonathan wrote: >> Is there a cool way to do this without calling the function twice?: >> >> a = func(b) unless func(b).nil? (AKA) >> a = func(b) if func(b) greg wrote: > assuming a is already defined > > class Object; def yield; yield(self) end end > > func(b).yield{|fb| a = fb if fb} Shades of forth: class Object; def if?; yield(self) if self end end a = 1 "fred wilma"[/fred/].if?{|x| a = x} p a # ==> "fred" "fred wilma"[/barney/].if?{|x| a = x} p a # ==> "fred" Btw, I think this construct has appeared before, but I can't seem to recall what it was called... -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407