From: Joseph McDonald Date: 2002-09-11T04:57:22+09:00 Subject: Re: Refactoring Ruby RK> class Foo RK> def [](index) RK> puts "Warning...[] called from #{caller}" RK> get_with_index(index) RK> end RK> def get_with_index(index) RK> #do something different RK> end RK> end Cool idea. You could probably take it a step further and have it rewrite the code at that point. To do it right you would probably have to have a parser that can parse ruby code. Here's a dangerous hack that can easily break code: class Foo def [](index) puts "Warning...[] called from #{caller}" # now fix the offending line in the file. caller_file, caller_line = caller[0].split(":") caller_line = caller_line.to_i - 1 # each_with_index zero based File.open("#{caller_file}.new", "w") do |newfile| File.open(caller_file,"r").each_with_index do |line, lineno| if caller_line == lineno newfile.print "# XXX CHANGED FROM: #{line}" line.gsub!(/\[#{index}\]/,".get_with_index(#{index})") end newfile.print line end end # could be dangerous... File.rename(caller_file, "#{caller_file}}.old") File.rename("#{caller_file}.new", caller_file) get_with_index(index) end def get_with_index(index) #do something different end end f = Foo.new blah = f[10] regards, -joe