Ruby tracking file loading and Class Definition

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.