http://hooopo.iteye.com/blog/604975
autoload可以加载(注册)任何模块的类或常量,不能有::操作符的...
特别广泛应用在如下代码结构:
#rack.rb #这样的文件结构ruby源码里随处可见 module Rack autoload :Builder, "rack/builder" autoload :Cascade, "rack/cascade" autoload :Chunked, "rack/chunked" autoload :CommonLogger, "rack/commonlogger" autoload :ConditionalGet, "rack/conditionalget" autoload :Config, "rack/config" ... end autoload本质是会调用Kernel.require,但是又和require有区别。 可以说autoload是一个smart的require...比require更加智能灵活。Just-in-Time.. 几个简单的例子: 要被require的文件,mylibrary.rb
- puts "I was loaded!"
- class MyLibrary
- end
puts "I was loaded!" class MyLibrary end
require mylibrary时文件立即执行。
- irb(main): 001 : 0 > require 'mylibrary'
- I was loaded!
- => true
irb(main):001:0> require 'mylibrary' I was loaded! => true
使用autoload,只有使用到需要的常量或类文件才被加载。。我们真正需要用某个文件时才加载,而require是直接加载,不管你是否会用到。
- irb(main):001:0> autoload :MyLibrary, 'mylibrary'
- => nil
- irb(main):002:0> MyLibrary.new
- I was loaded!
- => #<MyLibrary:0x0b1jef>
irb(main):001:0> autoload :MyLibrary, 'mylibrary' => nil irb(main):002:0> MyLibrary.new I was loaded! => #<MyLibrary:0x0b1jef>
其实,我觉得require更实用一些。。预先一次性加载来的痛快嘛。。
这个作者举了一个实例的例子来说明autoload的应用场景:http://www.subelsky.com/2008/05/using-rubys-autoload-method-to.html
link:http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
link:http://www.ruby-doc.org/core/classes/Kernel.html#M005968
link:http://www.ruby-forum.com/topic/172385
link:http://www.subelsky.com/2008/05/using-rubys-autoload-method-to.html
trap
trap("SIGINT") do irb.signal_handle end
捕捉信号量。然后执行block
Signal.trap(0, proc { puts "Terminating: #{$$}" }) Signal.trap("CLD") { puts "Child died" }
test:
# test(int_cmd, file1 [, file2] ) => obj # # # Uses the integer <i>aCmd</i> to perform various tests on # <i>file1</i> (first table below) or on <i>file1</i> and # <i>file2</i> (second table). # # File tests on a single file: # # Test Returns Meaning # ?A | Time | Last access time for file1 # ?b | boolean | True if file1 is a block device # ?c | boolean | True if file1 is a character device # ?C | Time | Last change time for file1 # ?d | boolean | True if file1 exists and is a directory # ?e | boolean | True if file1 exists # ?f | boolean | True if file1 exists and is a regular file # ?g | boolean | True if file1 has the \CF{setgid} bit # | | set (false under NT) # ?G | boolean | True if file1 exists and has a group # | | ownership equal to the caller's group # ?k | boolean | True if file1 exists and has the sticky bit set # ?l | boolean | True if file1 exists and is a symbolic link # ?M | Time | Last modification time for file1 # ?o | boolean | True if file1 exists and is owned by # | | the caller's effective uid # ?O | boolean | True if file1 exists and is owned by # | | the caller's real uid # ?p | boolean | True if file1 exists and is a fifo # ?r | boolean | True if file1 is readable by the effective # | | uid/gid of the caller # ?R | boolean | True if file is readable by the real # | | uid/gid of the caller # ?s | int/nil | If file1 has nonzero size, return the size, # | | otherwise return nil # ?S | boolean | True if file1 exists and is a socket # ?u | boolean | True if file1 has the setuid bit set # ?w | boolean | True if file1 exists and is writable by # | | the effective uid/gid # ?W | boolean | True if file1 exists and is writable by # | | the real uid/gid # ?x | boolean | True if file1 exists and is executable by # | | the effective uid/gid # ?X | boolean | True if file1 exists and is executable by # | | the real uid/gid # ?z | boolean | True if file1 exists and has a zero length # # Tests that take two files: # # ?- | boolean | True if file1 and file2 are identical # ?= | boolean | True if the modification times of file1 # | | and file2 are equal # ?< | boolean | True if the modification time of file1 # | | is prior to that of file2 # ?> | boolean | True if the modification time of file1 # | | is after that of file2 # # def test(int_cmd, file1, file2 ) # This is just a stub for a builtin Ruby method. # See the top of this file for more info. end
irb(main):045:0> test("A", "/var/log/mysql.log") => Fri Apr 02 10:22:46 +0800 2010 irb(main):046:0> test("C", "/var/log/mysql.log") => Fri Apr 02 10:22:46 +0800 2010 irb(main):047:0> test("c", "/var/log/mysql.log") => false irb(main):048:0> test("e", "/var/log/mysql.log") => true
这个可以用来调试。。很强大:
set_trace_func proc { |event, file, line, id, binding, classname| # printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname # }
rackuphttp://www.elanso.com/ArticleModule/sourcearticle.aspx?idx=L9IYPUNOVwLmP0KUG9SEKAIi