Hi --
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
That's just a re-assignment to the variable 'o'. It's not going to
have any impact on opt.option1.
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?
The block syntax seems a little odd. Is there any reason not to just
use attr_accessor? If you want it to default to a new instance of some
specific class, you could do something like:
class Options
def self.define_an_option(name, klass)
attr_writer name
iv = "@#{name}"
define_method(name) do
unless instance_variables.include?(iv)
instance_variable_set(iv, klass.new)
end
val = instance_variable_get(iv)
yield val if block_given? # if you really need this
val
end
end
define_an_option :option1, String
define_an_option :option2, Array
end
(and I suspect there are other versions of attr_reader with a default
value out there somewhere).
David
···
On Wed, 26 Sep 2007, Emmanuel Oga wrote:
--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!