From: Morton Goldberg Date: 2007-09-26T07:35:27+09:00 Subject: Re: Using a block to configure/initialize a class On Sep 25, 2007, at 6:09 PM, Emmanuel Oga wrote: > Hi! i need a solution for this, it's pretty simple but i can't get > it to > work... > > suppose: > > class Options > > def self.define_an_option > # ...don't-know-how-implementation :( > end > > define_an_option :option1, String > define_an_option :option2, Array > end > > Then, i want to do something like this: > > opt= Options.new > opt.option1 => "" > opt.option2 => [] > > opt.option1 do |o| > o= "Hello" > end > > opt.option2 do |o| > o << 10 > end > > options.option1 => "Hello" > options.option2 => [10] > > Thats all! Seems pretty simple, but i tried with a lot of variants > and I > don't get it to work. Any ideas? Have you looked at the built-in class Struct? I think it will supply what you need. Options = Struct.new(:option1, :option2) opt=Options.new("", []) opt.option1 = "Hello" opt.option2 << 10 << 42 opt.option1 # => "Hello" opt.option2 # => [10, 42] Regards, Morton