From: Gavin Sinclair Date: 2004-06-10T14:05:47+09:00 Subject: Re: [ANN] Active Record 0.8.3: Modules, mapping, and transactions John W. Long wrote: >> * Create new Active Record objects by setting the attributes through a >> block. Like this: >> >> person = Person.new do |p| >> p.name = 'Freddy' >> p.age = 19 >> end > > Why is the better than: > > person = Person.new( > 'name' => 'David', > 'age' => 12 > ) It's easier to type for a start. Although the aesthetics of it are subjective, it has some general advantages as an idiom: * #initialize doesn't have to do any hash processing to support it * since you're calling methods, the behaviour of 'name=' is respected * it's self-documenting; you look at the list of attributes of the class and know what you can specify in the constructor As it happens, none of these advantages apply to ActiveRecord: the method has to do hash processing anyway, there is no method name= (it's a shallow method_missing trick), and no attributes are documented. But it's IMO a good idiom and I'm happy to see it apply to ActiveRecord. Note that it takes a single line of code to support: yielf self if block_given? Gavin