ruby中的autoload

http://hooopo.iteye.com/blog/604975

autoload可以加载(注册)任何模块的类或常量,不能有::操作符的...
特别广泛应用在如下代码结构:

Ruby代码
  1. #rack.rb   
  2. #这样的文件结构ruby源码里随处可见   
  3. module  Rack  
  4.   autoload :Builder, "rack/builder"   
  5.   autoload :Cascade, "rack/cascade"   
  6.   autoload :Chunked, "rack/chunked"   
  7.   autoload :CommonLogger, "rack/commonlogger"   
  8.   autoload :ConditionalGet, "rack/conditionalget"   
  9.   autoload :Config, "rack/config"   
  10.   ...  
  11. end   
  12. autoload本质是会调用Kernel.require,但是又和require有区别。  
  13. 可以说autoload是一个smart的require...比require更加智能灵活。Just-in - Time ..  
  14. 几个简单的例子:  
  15.   
  16. 要被require的文件,mylibrary.rb  
  17. <pre name="code"   class = "ruby" >puts  "I was loaded!"   
  18.   
  19. class  MyLibrary  
  20. end </pre>  
  21. <br>require mylibrary时文件立即执行。  
  22. <br><pre name="code"   class = "irb" >irb(main):001:0> require  'mylibrary'   
  23. I was loaded!  
  24. => true   
  25. </pre>  
  26. <br>  
  27. <br>使用autoload,只有使用到需要的常量或类文件才被加载。。我们真正需要用某个文件时才加载,而require是直接加载,不管你是否会用到。  
  28. <br><pre name="code"   class = "ruby" >irb(main):001:0> autoload :MyLibrary,  'mylibrary'   
  29. => nil   
  30. irb(main):002:0> MyLibrary.new   
  31. I was loaded!  
  32. => #<MyLibrary:0x0b1jef></pre>   
  33. <br>  
  34. <br>其实,我觉得require更实用一些。。预先一次性加载来的痛快嘛。。  
  35. <br>这个作者举了一个实例的例子来说明autoload的应用场景:<a href="http://www.subelsky.com/2008/05/using-rubys-autoload-method-to.html"  target= "_blank" >http://www.subelsky.com/2008/05/using-rubys-autoload-method-to.html</a>  
  36. <br>  
  37. <br>  
  38. <br>  
  39. <br>  
#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

 
 
Ruby代码
  1. puts  "I was loaded!"   
  2.   
  3. class  MyLibrary  
  4. end   
puts "I was loaded!"

class MyLibrary
end

require mylibrary时文件立即执行。
Irb代码
  1. irb(main): 001 : 0 > require  'mylibrary'   
  2. I was loaded!  
  3. => true  
irb(main):001:0> require 'mylibrary'
I was loaded!
=> true


使用autoload,只有使用到需要的常量或类文件才被加载。。我们真正需要用某个文件时才加载,而require是直接加载,不管你是否会用到。
Ruby代码
  1. irb(main):001:0> autoload :MyLibrary,  'mylibrary'   
  2. => nil   
  3. irb(main):002:0> MyLibrary.new   
  4. I was loaded!  
  5. => #<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

Ruby代码
  1. trap( "SIGINT" do   
  2.   irb.signal_handle  
  3. end   
trap("SIGINT") do
  irb.signal_handle
end



捕捉信号量。然后执行block

Ruby代码
  1. Signal.trap(0, proc { puts  "Terminating: #{$$}"  })  
  2. Signal.trap("CLD" )  { puts  "Child died"  }  
Signal.trap(0, proc { puts "Terminating: #{$$}" })
Signal.trap("CLD")  { puts "Child died" }




test:

Ruby代码
  1. #     test(int_cmd, file1 [, file2] ) => obj   
  2.   #   
  3.   #   
  4.   # Uses the integer <i>aCmd</i> to perform various tests on   
  5.   # <i>file1</i> (first table below) or on <i>file1</i> and   
  6.   # <i>file2</i> (second table).   
  7.   #   
  8.   # File tests on a single file:   
  9.   #   
  10.   #   Test   Returns   Meaning   
  11.   #    ?A  | Time    | Last access time for file1   
  12.   #    ?b  | boolean | True if file1 is a block device   
  13.   #    ?c  | boolean | True if file1 is a character device   
  14.   #    ?C  | Time    | Last change time for file1   
  15.   #    ?d  | boolean | True if file1 exists and is a directory   
  16.   #    ?e  | boolean | True if file1 exists   
  17.   #    ?f  | boolean | True if file1 exists and is a regular file   
  18.   #    ?g  | boolean | True if file1 has the \CF{setgid} bit   
  19.   #        |         | set (false under NT)   
  20.   #    ?G  | boolean | True if file1 exists and has a group   
  21.   #        |         | ownership equal to the caller's group   
  22.   #    ?k  | boolean | True if file1 exists and has the sticky bit set   
  23.   #    ?l  | boolean | True if file1 exists and is a symbolic link   
  24.   #    ?M  | Time    | Last modification time for file1   
  25.   #    ?o  | boolean | True if file1 exists and is owned by   
  26.   #        |         | the caller's effective uid   
  27.   #    ?O  | boolean | True if file1 exists and is owned by   
  28.   #        |         | the caller's real uid   
  29.   #    ?p  | boolean | True if file1 exists and is a fifo   
  30.   #    ?r  | boolean | True if file1 is readable by the effective   
  31.   #        |         | uid/gid of the caller   
  32.   #    ?R  | boolean | True if file is readable by the real   
  33.   #        |         | uid/gid of the caller   
  34.   #    ?s  | int/nil | If file1 has nonzero size, return the size,   
  35.   #        |         | otherwise return nil   
  36.   #    ?S  | boolean | True if file1 exists and is a socket   
  37.   #    ?u  | boolean | True if file1 has the setuid bit set   
  38.   #    ?w  | boolean | True if file1 exists and is writable by   
  39.   #        |         | the effective uid/gid   
  40.   #    ?W  | boolean | True if file1 exists and is writable by   
  41.   #        |         | the real uid/gid   
  42.   #    ?x  | boolean | True if file1 exists and is executable by   
  43.   #        |         | the effective uid/gid   
  44.   #    ?X  | boolean | True if file1 exists and is executable by   
  45.   #        |         | the real uid/gid   
  46.   #    ?z  | boolean | True if file1 exists and has a zero length   
  47.   #   
  48.   # Tests that take two files:   
  49.   #   
  50.   #    ?-  | boolean | True if file1 and file2 are identical   
  51.   #    ?=  | boolean | True if the modification times of file1   
  52.   #        |         | and file2 are equal   
  53.   #    ?<  | boolean | True if the modification time of file1   
  54.   #        |         | is prior to that of file2   
  55.   #    ?>  | boolean | True if the modification time of file1   
  56.   #        |         | is after that of file2   
  57.   #   
  58.   #   
  59.   def  test(int_cmd, file1, file2 )  
  60.     # This is just a stub for a builtin Ruby method.   
  61.     # See the top of this file for more info.   
  62.   end   
#     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
Ruby代码
  1. irb(main):045:0> test( "A" "/var/log/mysql.log" )  
  2. => Fri Apr 02 10:22:46 +0800 2010  
  3. irb(main):046:0> test("C" "/var/log/mysql.log" )  
  4. => Fri Apr 02 10:22:46 +0800 2010  
  5. irb(main):047:0> test("c" "/var/log/mysql.log" )  
  6. => false   
  7. irb(main):048:0> test("e" "/var/log/mysql.log" )  
  8. => true   
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



这个可以用来调试。。很强大:

Ruby代码
  1. set_trace_func proc { |event, file, line, id, binding, classname|  
  2.   #        printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname   
  3.   #     }   
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

你可能感兴趣的:(C++,c,mysql,Ruby,rack)