From: Joel VanderWerf Date: 2002-11-14T05:10:40+09:00 Subject: Re: Keyword arguments? Austin Ziegler wrote: ... > Dave & Andy also noted that you can make it so that initialize > receives a block. Lyle recently implemented a change to the FXRuby > code that makes the technique cleaner (in that you don't have to use > 'self' all over the place), like so: > > class Person > attr_accessor :name, :age > > def initialize(name = nil, age = nil) > @name = name > @age = age > # ... default initialization > yield self if block_given? > end > end > > me = Person.new { |who| > who.name = 'austin' > who.age = 31 > } Cleaner, maybe, but there is a tradeoff. Both @name and @age must have writers for the above to work. With the other approach (block is handled with instance_eval), you can write: me = Person.new { @name = 'austin' @age = 31 } Either way, you're breaking encapsulation--by exposing writers, or by exposing the attrs themselves at the time of construction (only).