Forum: Ruby Fwd: Quiz #67 metakoans.rb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-19 17:56
(Received via mailing list)
Begin forwarded message:
615466ff7b9a15254d66d7ad27bed5ad?d=identicon&s=25 Michael Q. Harris (Guest)
on 2006-02-19 23:54
(Received via mailing list)
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
This topic is locked and can not be replied to.