- 浏览: 337331 次
- 性别:
- 来自: 北京
文章分类
最新评论
autoload
autoload可以加载(注册)任何模块的类或常量,不能有::操作符的...
特别广泛应用在如下代码结构:
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
捕捉信号量。然后执行block
test:
这个可以用来调试。。很强大:
rackuphttp://www.elanso.com/ArticleModule/sourcearticle.aspx?idx=L9IYPUNOVwLmP0KUG9SEKAIi
在rails的boot.rb里发现这句。。就是说要事先初始化一些常量啥的可以放到preinitialize.rb里,这个是比放在environment.rb里还要早。。
因为environment.rb也是在开头require 的boot.rb
又发现了这个:
in Rakefile:
因为rake只加载boot.rb而没有加载environment.rb所以要在rake里引用一些rails用的常量还是放到preinitialize.rb里比较好
其时也可以让rake task依赖environment这个rails自带的task,但是这样就会每次加载一次整个rails环境...
打算看看rack。。,我看东西很乱,没条理,开一大堆tab不知道看哪个好。。
开个贴把一些以前没用过的用法和link记录一下。
先上两个杯具站位。
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.rbputs "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
评论
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
8 楼
qichunren
2010-08-02
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
虎炮君你欺骗了我的感情……笔记就是俩杯具么?
发表评论
-
新博客
2012-04-23 20:47 1778https://db-china.org -
Ruby Verbose Warning Mode
2011-10-16 14:48 2058Ruby在很多方面是一个更优雅的Perl,从Perl社区继承了 ... -
Pattern Match In Ruby
2011-10-07 01:17 2018最近看了一些Erlang,模式匹配是个好东西,简单的sum函数 ... -
Draper: View Models for Rails
2011-10-07 01:19 2285Draper是一个Ruby gem,它让Rails model ... -
Active Record batch processing in parallel processes
2011-10-07 01:20 2280Active Record 提供 find_each来分批处理 ... -
最轻量级的Ruby后台任务
2011-08-04 16:47 3871普通情况下ruby调用系统命令行的过程是堵塞的,无论是用sys ... -
test
2011-07-15 19:59 0test -
fiber
2011-06-17 09:37 0挖坑,待填。。 1.用到fiber.alive?、fiber ... -
Identity Map in Rails3.1
2011-06-12 18:29 2749Identity Map是Rails3.1的又 ... -
xx00
2011-06-06 03:40 0https://github.com/ngmoco/cache ... -
挖坑1
2011-06-06 02:17 0cache money 源码 替换memcache为redis ... -
websocket demo
2011-06-04 20:44 2065地址:https://github.com/hooopo/we ... -
ruby GC
2011-06-02 04:24 0http://blog.csdn.net/lijun84/a ... -
reduce method missing call stack with dynamic define method
2011-04-22 22:54 1604method_missing是ruby里面一个非常cool的h ... -
Autocompete with Trie
2011-04-09 04:04 1685像微薄里面用户输入一 ... -
用imagemagick和tesseract-ocr破解简单验证码
2011-04-09 01:31 18964工具:imagemagick + tesseract-ocr ... -
OAuth gem for rails,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
2011-03-26 03:13 4493地址:https://github.com/hooopo/oa ... -
用jmeter模拟amf请求进行压力测试
2010-12-16 16:56 30321.获取amf二进制包: 在本地建立proxy,端口为888 ... -
Memoization in Ruby
2010-11-14 11:42 1225这里的Memoization就是将ruby的方法或lambda ... -
整理了一下2008-2010的RubyHeroes博客列表
2010-10-07 02:26 2841Bryan Helmkamp(webrat作者)https:/ ...
相关推荐
这篇源码阅读笔记将探讨 MySQL 的核心组件和关键特性。 首先,MySQL 的源码结构复杂,主要包括以下几个部分: 1. **SQL层**:这是MySQL与用户交互的接口,处理SQL语句的解析、预处理、优化和执行。解析器负责将SQL...
JDK源码阅读笔记
JDK源码阅读笔记
书籍会不定期更新,目前专注于k8s原始码的阅读,输出的阅读笔记会同步至多个平台,主要有以下几个: 本书github地址: : 在线阅读: : 个人博客: : 简书: 知乎专栏: 腾讯云—云+社区: 微信公众号:田飞雨 ...
在本文中,作者分享了对于Robot Framework这一自动化测试框架的源代码阅读笔记,这可能是一个系列文章的第一篇。Robot Framework是一个通用的开源自动化测试框架,主要用于验收测试和验收测试驱动开发(ATDD)。它使用...
开源项目源代码阅读笔记_OpenSourceReading
本文将深入探讨Spark的核心组件和工作原理,通过源码阅读笔记来解析Spark的内部机制。 首先,Spark的核心架构由Master、Worker和Driver三部分组成。Master节点在Spark集群中起到协调者的作用,它管理所有的Worker...
linux-Tcp IP源码阅读笔记.htm linux-Tcp IP源码阅读笔记.htm
《g2o源码阅读笔记:深入理解非线性优化框架在SLAM中的应用》 g2o(General Graph Optimization)是一个高效的优化库,广泛应用于视觉SLAM(Simultaneous Localization and Mapping)系统中,特别是在非线性优化的...
ArrayList源码阅读笔记 -- 介绍了ArrayList 普通增删改查的过程,从构造空参构造方法,然后添加元素,修改元素,删除元素,获取元素.
关于linux 内核中断相关的源码阅读笔记
AQS源码阅读笔记 AQS(AbstractQueuedSynchronizer)是Java并发编程中的一种同步器框架,它提供了一个队列来管理线程的排队和唤醒机制。下面是AQS源码阅读笔记的详细解释: 1. `ReentrantLock` 的 `unlock()` 方法...
**NASM汇编器源代码分析笔记** NASM(Netwide Assembler)是一款流行的、开源的x86汇编器,支持多种目标格式,如Intel 8086到最新的x86-64架构。它以其简洁的语法和广泛的平台兼容性而闻名。在深入分析nasm-0.98.39...
Dive into CPython internals, trying to illustrate every detail of CPython implementation | CPython 源码阅读笔记, 多图展示底层实现细节
小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本后端 (代码源)小程序源码 笔记本...
微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小程序 云笔记 (源码)微信小...
这篇源码阅读笔记深入探讨了CPython的内部工作原理,通过多图展示了许多底层实现的细节,帮助读者理解这个解释器如何将Python代码转化为机器可执行的指令。 在Python的世界里,"CPython"这个词通常指的是Python的...
该笔记并非源代码的详细讲解,亦非μC/OS-II的使用说明,而是汇总了阅读源码过程中产生的疑问及解答,进而从中归纳总结出μC/OS-II系统的内在机理,对于想从本质和源头探索操作系统的程序猿或许有点参考帮助,或许...
Spring源码阅读笔记 以Spring 4.2.9版本为基础,进行以下探索。 1.1 Spring是如何处理事务的 即先@Transactional注解是如何完成的。我们先创建一个类TestService,加上@Service和@Transactional注解,随便写一个保存...
go源码阅读笔记(math.1)func Abs(x float64) float64// Abs returns the absolute value of