--huq684BweRXVnRxX
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Great quiz Ara, and here's my solution. For some reason I wasn't using
instance_eval correctly, so I ended up defining an initalizer method for
the attribute. Also, I seem to have implemented the attr? method in a
manner different than most folks.
enjoy,
-jeremy
--
Jeremy Hinegardner jeremy / hinegardner.org
--huq684BweRXVnRxX
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="ruby-quiz_67.rb"
# ruby quiz #67 - knowledge.rb
#
# Jeremy Hinegardner
class Module
def _make_attribute(name,value l)
at @#{name}"
sym t.to_sym
name_init _#{name}_init"
if value.respond_to?("call") then
define_method(name_init,value)
end
define_method(name) do
if not self.instance_variables.include?(at) then
initial_value alue
if self.methods.include?(name_init) then
initial_value elf.send(name_init)
end
self.instance_variable_set(sym,initial_value)
end
self.instance_variable_get(sym)
end
define_method("#{name} { |val| self.instance_variable_set(sym,val) }
define_method("#{name}?") { not [false, nil].include?(self.instance_variable_get(sym)) }
end
def attribute(*args, &block)
args.each do |arg|
if arg.kind_of?(Hash) then
arg.each_pair do |k,v|
if block_given? then
_make_attribute(k,block)
else
_make_attribute(k,v)
end
end
else
_make_attribute(arg,block)
end
end
end
end
--huq684BweRXVnRxX--