Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Solution][QUIZ] #67 metakoans.rb

Reply
Thread Tools

[Solution][QUIZ] #67 metakoans.rb

 
 
Ari Brown
Guest
Posts: n/a
 
      10-06-2007
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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments