From: Gavin Sinclair Date: 2003-01-13T00:08:24+09:00 Subject: Re: Instance vars as arguments to initialize()? On Monday, January 13, 2003, 12:11:34 AM, Gavin wrote: > Now, even though it's untested, I'm pretty sure it wouldn't work, cos > I left a hole for you to fill :) (i.e. I started it and it became too > hard) OK, I've done it. Run this: class Class # Define initialize in terms of the symbols contained in *syms. # e.g. autoinit :a, :b => # def initialize(a, b) # @a = a # @b = b # end def autoinit(*syms) vars = syms.map { |s| s.to_s } params = vars.join "," body = vars.map { |v| "@#{v} = #{v}"}.join "; " code = <<-EOF def initialize(#{params}) #{body} end EOF module_eval code end end class Example autoinit :a, :b, :c end p Example.new(1,2,3) And then check out www.ruby-talk.org/11663, where I've just realised Avi has done the same thing but better. Gavin