[Solution] QAPrototype (#91)

E

Erik Veenstra

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

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
 

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,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top