[SOLUTION] Quiz #67 metakoans.rb

From: "Phil Tomson" <philtomson@gmail.com>
Date: February 18, 2006 6:46:02 PM CST
To: submission@rubyquiz.com
Subject: [SOLUTION] Quiz #67 metakoans.rb

James,

Fun quiz!

I don't subscribe to ruby-talk and the c.l.r bridge has been a bit
flakey (I didn't see your original post for this quiz on c.l.r, only
the responses) and I'll be out all day tomorrow.... so I include my
solution below.

Phil

#knowledge.rb
class Module
  def attribute(h_or_s, &b)
    if h_or_s.class == Hash
      str, val = h_or_s.keys[0].to_s, h_or_s.values[0]
    elsif h_or_s.class == String
      str, val = h_or_s.to_s, 0
    end
    if block_given?
      val = :init_block
      define_method(val, &b)
    end
    class_eval "def #{str}; @#{str}||=#{val} ; end"
    class_eval "def #{str}=(val); @#{str}=val; end"
    class_eval "def #{str}?; @#{str} ? true : false ; end"
  end
end

···

Begin forwarded message:

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