From: James Edward Gray II Date: 2007-10-09T21:04:38+09:00 Subject: Fwd: Quiz 67 Solution Begin forwarded message: > From: "Sudheer Koganti" > Date: October 9, 2007 12:16:13 AM CDT > To: submission@rubyquiz.com > Subject: Quiz 67 Solution > > Hi, > > I spent 3 days figuring out this solution. Though this quiz is way > past, I'd like to send this solution anyway. > > class Module > def attribute(arg, &block) > a = arg.is_a?(Hash) ? arg.keys.first : arg > p = proc do |me| > if me.instance_variable_defined?("@#{a}") then > me.instance_variable_get("@#{a}") > elsif arg.is_a?(Hash) > arg.values.first > elsif block_given? > me.instance_eval(&block) > else nil end > end > define_method("#{a}") { p.call(self) } > define_method("#{a}?") { p.call (self) ? true : false } > define_method("#{a}=") { |val| instance_variable_set("@#{a}", > val) } > end > end > > > Thanks > Sudheer > >