From: "Sudheer Koganti" <sbkoganti@gmail.com>
Date: October 9, 2007 12:16:13 AM CDT
To: submission@rubyquiz.com
Subject: Quiz 67 SolutionHi,
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
endThanks
Sudheer
···
Begin forwarded message: