Here is my solution, it focuses on being able to use the methods you input to patch the original file. For inputting methods, you need to give the entire method declaration (or any code that adds it, like attr_*). The script automatically detects when you're done, except when you have a syntax error (then you need to force input with "END"). An irb session: 91test.rb is a file with require 'inputmethod.rb' class Test include InputMethod end >irb -r91test.rb irb(main):001:0> t = Test.new irb(main):002:0> t.foo Give the method definition for Test#foo END to force input end, display error and return nil > def foo > bar > end Give the method definition for Test#bar END to force input end, display error and return nil > attr_accessor :bar => nil irb(main):003:0> t.bar=42 ; t.foo => 42 irb(main):004:0> puts Test.patches def foo bar end attr_accessor :bar irb(main):005:0> Test.remove_patch :foo irb(main):006:0> t.test Give the method definition for Test#test END to force input end, display error and return nil > def test(x=42) > > if x < 20 > "smaller" > else > "greater or equal" > end > end => "greater or equal" irb(main):007:0> Test.patch! Adding methods bar,test to Test 91test.rb is now: class Test attr_accessor :bar def test(x=42) if x < 20 "smaller" else "greater or equal" end end include InputMethod end Even the indent is added to fit the class definition. :) Pastie: http://pastie.caboo.se/9396 Code: require 'facets' require 'facets/core/kernel/singleton' class Module public :class_variable_set, :class_variable_get # public in 1.9 (?) end module InputMethod def self.included(klass) klass.class_variable_set(:@@inputmethods, methods=Hash.new{|h,k| h[k]=[]}) include_point = caller.first klass.singleton.send(:define_method,:patch!) { return if methods.empty? _, file, line = /(.*)\:(\d+)/.match(include_point).to_a puts "Adding methods #{methods.keys.join(',')} to #{klass}" contents = File.readlines(file) ix = line.to_i-1 indent = contents[ix][/^\s*/] code = methods.values.join("\n").split("\n").map{|l| indent + l }.join("\n") # add indent contents[ix] = code + "\n\n" + contents[ix] # insert methods before include statement File.open(file,'w') {|f| f << contents.join } methods.clear } klass.singleton.send(:define_method,:patches) { methods.values.join("\n") } klass.singleton.send(:define_method,:remove_patch) {|m| methods.delete m remove_method m } end def method_missing(method,*args) print "Give the method definition for #{self.class}\##{method}\nEND to force input end, display error and return nil\n> "; method_body = line = gets begin self.class.class_eval(method_body) rescue SyntaxError return puts("Syntax error: #{$!.message[/[^\:]+\Z/].lstrip}") if line.chomp == 'END' print '> ' method_body << line=gets retry end self.class.class_variable_get(:@@inputmethods)[method] = method_body send(method,*args) end end