[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[Solution] QAPrototype (#91

Erik Veenstra

8/22/2006 8:53:00 PM

----------------------------------------------------------------

module QAPrototype
def method_missing(method_name)
puts "#{method_name} is undefined"
puts "Please define what I should do (end with a newline):"

(code ||= "") << (line = gets) until line and line.chomp.empty?

self.class.module_eval do
define_method(method_name){eval code}
end

at_exit do
puts ""
puts "class #{self.class}"
puts " def #{method_name}"
code.gsub(/[\r\n]+$/, "").split(/\r*\n/).each{|s| puts " "*4+s}
puts " end"
puts "end"
end
end
end

----------------------------------------------------------------

class Foo
include QAPrototype
end

3.times do
foo = Foo.new

foo.bar
foo.baz
end

----------------------------------------------------------------

$ ruby test.rb
bar is undefined
Please define what I should do (end with a newline):
@x = 7
@y = 8
@z = self.object_id

baz is undefined
Please define what I should do (end with a newline):
p inspect

"#<Foo:0xb7d7ebfc @x=7, @z=-605293058, @y=8> "
"#<Foo:0xb7d7e954 @x=7, @z=-605293398, @y=8> "

class Foo
def baz
p inspect
end
end

class Foo
def bar
@x = 7
@y = 8
@z = self.object_id
end
end

----------------------------------------------------------------

gegroet,
Erik V. - http://www.erikve...