Here is mine. Nothing too extraordinary or different than other
solutions, but it is fairly short and readable.
class Module
def attribute(x, &block)
name, value = x.to_a[0] # produces a warning is x is a symbol
ivar = "@#{name}"
define_method(name) do
if instance_variables.include?(ivar)
instance_variable_get(ivar)
else
value || (instance_eval &block if block)
end
end
attr_writer name
define_method("#{name}?") { !!send(name) }
end
end
As others have said, this was a cool quiz. While the meta-programming
part was interesting, I think this quiz is a better advertisement for
test-first coding than anything! Very cool and fun test suite. Thanks
Ara.
Ryan