Joel VanderWerf
11/16/2006 1:41:00 AM
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
Otherwise you have to do this:
def foo
unless @foo
@foo = Foo.new
@foo.bar = "bar"
end
@foo
end
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407