[Solution][QUIZ] #67 metakoans.rb

A

Ari Brown

Here is my solution: I got stuck on like 6, so I borrowed a line and
a half from Adam Shelly.

I understand it looks a lot like his code, but I swear, i only
borrowed 12 and part of 13 from him!

#!/usr/local/bin/ruby
#
# Created by me on 2007-10-05.
# Some code (lines 12, and part of 13) borrowed from Adam Shelly
# Copyright (c) 2007. All pwnage reserved.


class Object
def attribute(*vars, &b)
vars.each do |var|
var.each_pair{|k,v| attribute [k,v] } and next if var.is_a? Hash
var, v = var # Idea borrowed [stolen] from Adam Shelly.
Brilliant, I might add!
class_eval "def #{var}; @#{var} = (defined?(@#{var}) ? @#
{var} : #{var}_proc); end" # Set up getters
attr_writer var.to_sym # setters
class_eval "def #{var}?; return (self.#{var}.nil? ? false :
true ); end" # and queries
define_method "#{var}_proc", b || Proc.new {v}
end
end
end


Ari Brown
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top