From: Mike Harris Date: 2006-02-21T23:31:33+09:00 Subject: Re: [SOLUTION] metakoans.rb (#67) --------------010205020803020604040803 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit The only interesting thing I have to say here that hasn't been said is that this test seems like a variation of Pair Programming, where one person writes the tests and the other writes the code. No better way to ensure you only write code to make a failing test pass than to know nothing about the problem space except the most recent test failure. class Object def attribute(arg, &block) name = (arg.class == Hash ? arg.keys[0] : arg) define_method(name) do first_access_action(arg,&block) unless instance_eval("defined? " + "@" + name) instance_variable_get "@" + name end attr_writer name alias_method name+"?",name end def first_access_action(arg,&block) name = (arg.class == Hash ? arg.keys[0] : arg) send(name+"=",instance_eval(&block)) if block_given? send(name+"=",arg.values[0]) if arg.class == Hash end end --------------010205020803020604040803 Content-Type: text/plain; name="knowledge.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="knowledge.rb" class Object def attribute(arg, &block) name = (arg.class == Hash ? arg.keys[0] : arg) define_method(name) do first_access_action(arg,&block) unless instance_eval("defined? " + "@" + name) instance_variable_get "@" + name end attr_writer name alias_method name+"?",name end def first_access_action(arg,&block) name = (arg.class == Hash ? arg.keys[0] : arg) send(name+"=",instance_eval(&block)) if block_given? send(name+"=",arg.values[0]) if arg.class == Hash end end --------------010205020803020604040803--