[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [SOLUTION] metakoans.rb (#67

email55555 email55555

2/22/2006 8:08:00 PM

Just coded to pass all koans.
The program only accepts "one" attribute. (it only accepts the first
one, others will be ignore)
There are no koan to say what kind of behavior for multi-attribute
with hash table and block given at the same time.


class Module
def attribute(hash, &block)

(name, default) = hash.is_a?(Hash) ? [hash.keys[0],
hash.values[0]] : [hash, nil]

define_method("#{name}?") { instance_eval("@#{name} != nil") }

define_method("#{name}") do
if !instance_variables.include?( "@#{name}" )
instance_variable_set("@#{name}", block.nil? ? default :
instance_eval(&block))
else
instance_variable_get("@#{name}")
end
end

class_eval <<-METHOD
def #{name}=(value)
@#{name} = value
end
METHOD

end
end


--
www.doublegifts.com