From: Eric Mahurin Date: 2005-08-19T00:36:56+09:00 Subject: Re: polymorphism and/or named parameters: the ruby way? --- James Edward Gray II wrote: > On Aug 8, 2005, at 12:21 PM, Eric Mahurin wrote: > > > --- "Berger, Daniel" wrote: > >> > >> A typical idiom for constructors is to do "yield self if > >> block_given?", > >> which I prefer over hash based arguments. For example: > >> > >> class Foo > >> attr_accessor :bar, :baz, :zap > >> def initialize > >> yield self if block_given? > >> end > >> end > >> > >> foo = Foo.new do |f| > >> f.bar = "hello" > >> f.baz = 5 > >> f.zap = "world" > >> end > >> > > > > What's the advantage of this over: > > > > foo = Foo.new > > foo.bar = "hello" > > foo.baz = 5 > > foo.zap = "world" > > Perhaps you want to create and use an object without even > assigning > it to a variable: > > Message do |mes| > # build message here... > > mes.send > end You missed a .new, I believe. I'd still say my Object#post (someone else called it "then") would be a better thing to have instead of every initialize doing "yield self if block_given?" at the end. If there was something done after the yield (like checking or even closing/destroying the object), then there'd be more use. class Object def post yield(self) self end end Message.new.post do |mes| # build message here... mes.send end ____________________________________________________ Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs