From: Joel VanderWerf Date: 2006-11-17T06:59:42+09:00 Subject: Re: #returning and #tap ara.t.howard@noaa.gov wrote: > On Fri, 17 Nov 2006, Eric Hodel wrote: > >>> Otherwise you have to do this: >> >> I would write it: >> >> def foo >> return @foo if @foo >> @foo = Foo.new >> @foo.bar = 'bar' >> @foo >> end >> >> $ wc >> ... >> 6 14 77 >> >> Ten characters and one line more typing, but less punctuation. (And >> those ten extra characters will be mostly be handled by my tab key.) > > and quite easy to golf if line count matters that much > > def foo > @foo ||= Foo.new.instance_eval{ self.bar = 'bar'; self } > end > > -a ROFL Coming full circle around the golf course... This last one above is what lead me to prefer #then (== #tap) in the first place: def foo @foo ||= Foo.new.then do |f| f.bar = "bar" end end Also, #then avoids the scoping nastiness of #instance_eval. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407