`

Ruby中require、load、autoload的区别

阅读更多
require:
require(filename) -> true or false
Ruby tries to load the library named string, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ``.rb’’, it is loaded as a source file; if the extension is ``.so’’, ``.o’’, or ``.dll’’, or whatever the default shared library extension is on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ``.rb’’, ``.so’’, and so on to the name. The name of the loaded feature is added to the array in $". A feature will not be loaded if its name already appears in $". The file name is converted to an absolute path, so ``require 'a'; require './a'’’ will not load a.rb twice.

个人翻译:

   Ruby 会尝试去根据require(filename)中的filename去加载库,如果加载成功即返回tru。如果filename不能解析成一个绝对路径,它会直接在$:.中查找。如果加载文件有“.rb”的扩展名,将会被加载成资源文件,如果是“.so”, ".o"或者是“.dll”,或者在当前平台下任何的可共享的库扩展名,Ruby都会将其当成Ruby的扩展资源进行加载。否则,Ruby会尝试将“.rb”,".so"等等拼接在filename。加载过后的文件特性将会被添加进$的数组中。如果在$数组中,某个特性的名字已经存在,将不会重复被加载。文件名也会被转换为绝对路径,所以``require 'a'; require './a'’’不会再第二次加载a.rb。

 

个人理解: require也就是只会加载一次,第一次加载成功则返回true,重复加载则会返回faluse。

 

load: 

load(filename, wrap=false) → true 写道
Loads and executes the Ruby program in the file filename. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program’s global namespace. In no circumstance will any local variables in the loaded file be propagated to the loading environment.

 个人翻译:加载并执行在文件里的Ruby程序。如果filename不能解析成一个绝对路径,它会直接在$:.中查找。 wrap是可选参数,默认为false,如果设为true,则这个文件将在匿名模块下运行,从而包括调用者的名字空间。任何filename里面的局部变量在装载它的环境下是不可用的。

 

个人理解:加载并执行,而且 load是每次都会重新加载。只要你的代码有修改,不用通过重启服务器也可以直接重新加载你修改过的内容。

 

autoload: 

autoload(module, filename) → nil 写道
Registers filename to be loaded (using Kernel::require) the first time that module (which may be a String or a symbol) is accessed.

 个人翻译: 在第一次访问module(可能是字符串或者是符号)时,filename将会被加载。即理解为:第一使用时才会加载该文件,可以理解为懒加载。

 

分享到:
评论

相关推荐

    Ruby中require、load、include、extend的区别介绍

    require,load用于文件,如.rb等等结尾的文件。include,load则用于包含一个文件中的模块。 require 一般情况下用于加载库文件,而load则用于加载配置文件。 1、require:加载一个库,并且只加载一次,如果多次加载会...

    举例讲解Ruby中require的使用方法

    主要介绍了Ruby中require的使用方法,用其来各种引用文件路径,需要的朋友可以参考下

    Ruby,详细描述require背后的故事

    在Ruby中,`require`和`require_relative`是两种常见的加载方法。`require`默认情况下不会重复加载同一文件,这是通过维护一个已加载文件的列表来实现的。而`require_relative`则是根据当前文件的路径来解析目标文件...

    Laravel访问出错提示:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or di解决方法

    本文实例讲述了Laravel框架访问...`Warning: require(/http/www.mywakavLee.cn/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /http/www.mywakavLee.cn/bootstrap/autolo

    unite-ruby-require.vim:用于搜索需要的 gem 的 unite.vim 源

    这个 Vim 插件是一个源代码,用于在 Ruby 中搜索require路径。 这个插件会搜索 Ruby 标准库路径、gem 的默认路径和 bundler 的本地路径。 一切都是异步完成的,它也会缓存。 用法 :Unite ruby /require 如果你有很...

    ruby example webrick

    ruby sample: @echo off cd D:\proj\demo rem previously drop tables call rake db:migrate VERSION=0 rem load test data (it depends on the db:environment and db:migrate tasks) call rake db:load_test_...

    ruby-pingyin ruby中将中文转化成拼音

    ruby-pinyin。现在介绍给大家。 里面自带api。用法也很简单。 在rails 中 把 pinyin.rb 及 dict 放到lib目录中。 在模型中引入文件。 require "pinyin" 按后实例化一个pinyin py = PinYin.instance py.to_...

    include和require的区别

    详细介绍了include和require的区别

    【跳坑日记】Ubuntu 16.04安装 Ruby2.7.0遇到的坑:cannot load such file — openssl (LoadError)

    通过源码安装Ruby 2.7.0版本时报错,内容:ruby-2.7.0/lib/rubygems/core_ext/kernel_require.rb:92:in 'require': cannot load such file -- openssl (LoadError) 处理过程 前情提要:最近打算把自己的技术总结...

    ruby中文转拼音的api

    require "pinyin" 按后实例化一个pinyin py = PinYin.instance py.to_pinyin_abbr("刘德华") #=> ldh py.to_permlink('中文轉漢語拼音') #=> "zhong-wen-zhuan-han-yu-pin-yin" py.to_pinyin('中文轉漢語拼音'...

    ruby运行环境,ruby安装,windows版本

    windows版本ruby运行环境安装,下载后解压即可,直接双击运行

    ruby and watir 安装指南

    C:\>gem envRubyGems Environment: - RUBYGEMS VERSION: 1.3.7 - RUBY VERSION: 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] - INSTALLATION DIRECTORY: c:/ruby/lib/ruby/gems/1.8 - RUBY EXECUTABLE: c:/...

    详解Node中导入模块require和import的区别

    本篇文章主要介绍了详解Node中导入模块require和import的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

    require.js教程

    本书籍主要讲解了require.js如何使用,以及在使用过程中有哪些容易出问题的地方,适合工作中使用require.js开发的项目

    第六十八课:基于Ruby内存加载shellcode第一季1

    在本教程中,我们使用Ruby语言来加载生成的shellcode。我们首先 require 了必要的库,包括 `fiddle` 和 `fiddle/import`。 ```ruby require 'fiddle' require 'fiddle/import' require 'fiddle/types' ``` 然后,...

    node-loadbalance:蒸馏负载平衡引擎的集合

    安装使用可以: npm i loadbalance const loadbalance = require ( 'loadbalance' )用法要使用,实例化引擎或使用池调用工厂方法。 然后调用pick(),它将返回选定的对象,重复调用pick()会从池中产生相同或不同...

    详解小程序循环require之坑

    在JavaScript中,模块之间可能出现相互引用的情况,例如现在有三个模块,他们之间的相互引用关系如下,大致的引用关系可以表示为 A -> B -> C -> A,要完成模块A,它依赖于模块C,但是模块C反过来又依赖于模块A,...

Global site tag (gtag.js) - Google Analytics