From: Eric Hodel Date: 2006-11-17T05:04:30+09:00 Subject: Re: #returning and #tap On Nov 15, 2006, at 5:40 PM, Joel VanderWerf wrote: > Eric Hodel wrote: > ... >> So... it usually doesn't save any lines, it adds typing, it hides >> the object you're returning in the middle of a statement and it is >> significantly more expensive to execute[1]. (Oh, but maybe, in >> some cases, it might save you one or two lines of code.) >> No thanks. >> I don't think that makes it more rubyish. Every time I've >> encountered #returning in Rails I've found it decreased the >> readability of the code and uselessly increased the complexity. >> (Wait, now what does this return? oh, yeah, way up there.) > > This variant saves typing and IMO is more idiomatically ruby: > > class Object > def then > yield(self) > self > end > end > > def foo > @foo ||= Foo.new.then do |f| > f.bar = "bar" > end > end $ wc [...] 5 12 67 > 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.) -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com