The sane:
class Module
def attribute(arg, val=nil, &blk)
if arg.is_a?(Hash)
arg.each{|k,v| attribute(k,v)}
return
end
define_method(arg) do ||
if instance_variables.include?("@#{arg}")
instance_variable_get("@#{arg}")
else
blk ? instance_eval(&blk) : val
end
end
define_method("#{arg}?"){|| !send(arg).nil?}
attr_writer(arg)
end
end
The insane:
class Module
def attribute(a, &b)
b or return (Hash===a ? a : {a=>nil}).each{|k,v| attribute(k){v}}
define_method(a){(x=eval("@#{a}")) ? x[0] : instance_eval(&b)}
define_method("#{a}?"){!send(a).nil?}
define_method("#{a}="){|v| instance_variable_set("@#{a}", [v])}
end
end