Classtrace. RB:
#encoding: utf-8module ClassTrace T = [] #定义数组常量T,存放trace信息 if x = ARGV.index("--traceout") #如果ruby命令后面有--traceout参数,则记录到文件中,否则输出 OUT = File.open(ARGV[x + 1], ‘w‘) ARGV[x, 2] = nil else OUT = STDERR endendalias origin_require require #给require定义别名方法origin_require,下面要重新定义alias origin_load load #给load方法定义别名方法origin_load,下面要重新定义load方法def require(file) ClassTrace::T << [file, caller[0]] #将require方式加载的文件和加载该文件的位置放入一个数组,并添加到T常量中 origin_require(file) #require加载文件enddef load(*args) ClassTrace::T << [args[0], caller[0]] #将load方式加载的文件和加载该文件的位置放入一个数组 origin_load(*args) #load加载文件enddef Object.inherited(c) #定义钩子方法inherited方法,当有新的类定义时调用此方法将类名和定义的位置加入到T常量 ClassTrace::T << [c, caller[0]] endat_exit{ #当程序退出执行时 o = ClassTrace::OUT o.puts ‘=‘ * 60 o.puts ‘Files Loaded and Classes Defined:‘ o.puts ‘=‘ * 60 ClassTrace::T.each do |what, where| #遍历trace信息数组T,将信息写入到文件或输出 if what.is_a? Class o.puts "Defined: #{what.ancestors.join(‘<-‘)} at #{where}" else o.puts "Loaded: #{what} at #{where}" end end}
Index. Rb
#encoding: utf-8require ‘./classtrace‘require ‘./test‘class Test; endputs "11111"
Test. Rb
#encoding: utf-8class TestClass; end
Ruby index. RB -- traceout/tmp/trace # Write the trace information of the files and defined classes loaded in the index. RB file to the/tmp/trace file, as shown in the following figure:
========================================================== ================================
Files loaded and classes defined:
========================================================== ================================
Loaded:./test at index. RB: 3: In '<main>'
Defined: testclass <-object <-kernel <-basicobject at/users/fangxiang/work/Ruby/test. RB: 2: In '<top (required)>'
Defined: Test <-object <-kernel <-basicobject at index. RB: 4: In '<main>'
Ruby tracking file loading and Class Definition