Yep, that makes more sense. It looks a lot more useful when you do some operations after the yield. Kind of like the reason the IO.open block form is useful - it does the close for you. --- Florian Frank <flori / nixe.ping.de> wrote: > Eric Mahurin wrote: > > >What's the advantage of this over: > > > >foo = Foo.new > >foo.bar = "hello" > >foo.baz = 5 > >foo.zap = "world" > > > > > 1. To avoid, having to call a configure method after > configuration > parameters have been set (or before certain instance methods > are called), > 2. to guarantee, that no Foo objects are created, that aren't > configured > correctly. > > Consider this contrived example: > > class Foo > FIELDS = %w[bar baz zap] > > attr_accessor(*FIELDS) > > def initialize > if block_given? > yield self > configure > else > raise ArgumentError, "configuration required!" > end > end > > def configure > for f in FIELDS > __send__(f) or raise ArgumentError, "configuration for > #{f} missing!" > end > @greeting = [ @bar ] * @baz * ', ' + " #@zap!" > end > > def greet > puts @greeting > end > end > > f = Foo.new do |f| > f.bar = "hello" > f.baz = 5 > f.zap = "world" > end > f.greet > > After the configuration block has been executed, f is > guaranteed to be a > correctly configured Foo instance. (At least no parameter has > been > forgotten, but further checks are possible as well.) > > > ____________________________________________________ Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs