From: Joel VanderWerf Date: 2005-08-09T04:50:25+09:00 Subject: Re: polymorphism and/or named parameters: the ruby way? Eric Mahurin wrote: ... > Functionally, I guess something like this would be most > flexible (no performance advantage like "ensure"): > > class Object > def post > yield(self) > self > end > end > > Then you could do these types of things: > > a[i.post{i+=1}] > > foo = Foo.new.post do |f| > f.bar = "hello" > f.baz = 5 > f.zap = "world" > end > > With this, no need for the "yield(self) if block_given?" in > initialize. > > This very useful little method is a nice thing to have in your > bag-o-tricks. Nice. you could even call it #then: class Object def then yield(self) self end end #Then you could do these types of things: a = (0..10).to_a i = 3 p a[i.then{i+=1}] p i Foo = Struct.new :bar, :baz, :zap foo = Foo.new.then do |f| f.bar = "hello" f.baz = 5 f.zap = "world" end p foo __END__ output: 3 4 # -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407