`
Hooopo
  • 浏览: 337331 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

源码阅读笔记

    博客分类:
  • Ruby
阅读更多
autoload

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

require mylibrary时文件立即执行。
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>


其实,我觉得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

3
0
分享到:
评论
11 楼 qichunren 2010-08-02  
http://stackoverflow.com/questions/2837912/is-autoload-thread-safe-in-ruby-1-9
10 楼 qichunren 2010-08-02  
http://www.germanforblack.com/articles/ruby-autoload
9 楼 qichunren 2010-08-02  
http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
7 楼 Hooopo 2010-04-11  
def preinitialize
      load(preinitializer_path) if File.exist?(preinitializer_path)
    end

    def preinitializer_path
      "#{RAILS_ROOT}/config/preinitializer.rb"
    end


在rails的boot.rb里发现这句。。就是说要事先初始化一些常量啥的可以放到preinitialize.rb里,这个是比放在environment.rb里还要早。。
因为environment.rb也是在开头require 的boot.rb

又发现了这个:
in Rakefile:
require(File.join(File.dirname(__FILE__), 'config', 'boot'))


因为rake只加载boot.rb而没有加载environment.rb所以要在rake里引用一些rails用的常量还是放到preinitialize.rb里比较好

其时也可以让rake task依赖environment这个rails自带的task,但是这样就会每次加载一次整个rails环境...
6 楼 Hooopo 2010-04-04  
让两次rand相同:

irb(main):001:0> rand
=> 0.405568573131463
irb(main):002:0> rand
=> 0.557263248026071
irb(main):003:0> srand 1
=> 74077417124182359563213677588757462624
irb(main):004:0> rand
=> 0.417022004702574
irb(main):005:0> srand 1
=> 1
irb(main):006:0> rand
=> 0.417022004702574
irb(main):007:0> 
5 楼 Hooopo 2010-03-02  
在服务器上装了新版rack(1.1.0),,,,部署的时候thin启动不来。。。查了下原来就是这个问题:http://www.ruby-forum.com/topic/201603

好郁闷。。又卸载了。。
4 楼 Hooopo 2010-03-01  
RednaxelaFX 写道
这是啥源码的阅读啊,先透露点~

打算看看rack。。,我看东西很乱,没条理,开一大堆tab不知道看哪个好。。
开个贴把一些以前没用过的用法和link记录一下。
3 楼 RednaxelaFX 2010-03-01  
这是啥源码的阅读啊,先透露点~
2 楼 Hooopo 2010-03-01  
RednaxelaFX 写道
虎炮君你欺骗了我的感情……笔记就是俩杯具么?

先上两个杯具站位。 
1 楼 RednaxelaFX 2010-03-01  
虎炮君你欺骗了我的感情……笔记就是俩杯具么?

相关推荐

    mysql5.5.25 源码阅读笔记

    这篇源码阅读笔记将探讨 MySQL 的核心组件和关键特性。 首先,MySQL 的源码结构复杂,主要包括以下几个部分: 1. **SQL层**:这是MySQL与用户交互的接口,处理SQL语句的解析、预处理、优化和执行。解析器负责将SQL...

    JDK源码阅读笔记

    JDK源码阅读笔记

    JDK源码阅读笔记LearningJDK

    JDK源码阅读笔记

    源代码阅读笔记:源码阅读笔记

    书籍会不定期更新,目前专注于k8s原始码的阅读,输出的阅读笔记会同步至多个平台,主要有以下几个: 本书github地址: : 在线阅读: : 个人博客: : 简书: 知乎专栏: 腾讯云—云+社区: 微信公众号:田飞雨 ...

    Robot Framework 源代码阅读笔记 之 一 - CSDN博客

    在本文中,作者分享了对于Robot Framework这一自动化测试框架的源代码阅读笔记,这可能是一个系列文章的第一篇。Robot Framework是一个通用的开源自动化测试框架,主要用于验收测试和验收测试驱动开发(ATDD)。它使用...

    开源项目源代码阅读笔记_OpenSourceReading.zip

    开源项目源代码阅读笔记_OpenSourceReading

    spark源码阅读笔记

    本文将深入探讨Spark的核心组件和工作原理,通过源码阅读笔记来解析Spark的内部机制。 首先,Spark的核心架构由Master、Worker和Driver三部分组成。Master节点在Spark集群中起到协调者的作用,它管理所有的Worker...

    linux-Tcp IP源码阅读笔记.htm

    linux-Tcp IP源码阅读笔记.htm linux-Tcp IP源码阅读笔记.htm

    g2o源码阅读笔记;g2o源码阅读笔记

    《g2o源码阅读笔记:深入理解非线性优化框架在SLAM中的应用》 g2o(General Graph Optimization)是一个高效的优化库,广泛应用于视觉SLAM(Simultaneous Localization and Mapping)系统中,特别是在非线性优化的...

    ArrayList源码阅读笔记

    ArrayList源码阅读笔记 -- 介绍了ArrayList 普通增删改查的过程,从构造空参构造方法,然后添加元素,修改元素,删除元素,获取元素.

    linux 内核中断相关的源码阅读笔记

    关于linux 内核中断相关的源码阅读笔记

    AQS源码阅读笔记,画了两三天的AQS...

    AQS源码阅读笔记 AQS(AbstractQueuedSynchronizer)是Java并发编程中的一种同步器框架,它提供了一个队列来管理线程的排队和唤醒机制。下面是AQS源码阅读笔记的详细解释: 1. `ReentrantLock` 的 `unlock()` 方法...

    nasm 源代码分析笔记

    **NASM汇编器源代码分析笔记** NASM(Netwide Assembler)是一款流行的、开源的x86汇编器,支持多种目标格式,如Intel 8086到最新的x86-64架构。它以其简洁的语法和广泛的平台兼容性而闻名。在深入分析nasm-0.98.39...

    Python-CPython源码阅读笔记多图展示底层实现细节

    Dive into CPython internals, trying to illustrate every detail of CPython implementation | CPython 源码阅读笔记, 多图展示底层实现细节

    小程序源码 笔记本后端 (代码源)

    小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本...

    微信小程序 云笔记 (源码)

    微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小...

    CPython 源码阅读笔记, 多图展示底层实现细节

    这篇源码阅读笔记深入探讨了CPython的内部工作原理,通过多图展示了许多底层实现的细节,帮助读者理解这个解释器如何将Python代码转化为机器可执行的指令。 在Python的世界里,"CPython"这个词通常指的是Python的...

    ucOS-II源码阅读笔记-底层代码详细注解

    该笔记并非源代码的详细讲解,亦非μC/OS-II的使用说明,而是汇总了阅读源码过程中产生的疑问及解答,进而从中归纳总结出μC/OS-II系统的内在机理,对于想从本质和源头探索操作系统的程序猿或许有点参考帮助,或许...

    SpringCodeReadingNotes:Spring源码阅读笔记-spring源码阅读

    Spring源码阅读笔记 以Spring 4.2.9版本为基础,进行以下探索。 1.1 Spring是如何处理事务的 即先@Transactional注解是如何完成的。我们先创建一个类TestService,加上@Service和@Transactional注解,随便写一个保存...

    Miss-you#completedblog#go源码阅读笔记(math)1

    go源码阅读笔记(math.1)func Abs(x float64) float64// Abs returns the absolute value of

Global site tag (gtag.js) - Google Analytics