三者之间区别并不像你想的那么难,也不会像有些文章写的那么长。挺简单的。
相同之处:三者均在kernel中定义的,均含有包含进某物之意。
不同之处:
1、requre,load用于文件,如.rb等等结尾的文件。
2、include则用于包含一个文件(.rb等结尾的文件)中的模块。
3、requre一般情况下用于加载库文件,而load则用于加载配置文件。
4、requre加载一次,load可加载多次。
怎么样,简单吧!再看个例子。
如果说abc.rb中包含一个模块Ma,和几个类Ca,Cb等等。那么你若想在ef.rb文件中使用abc.rb中的资源,你得这样:
require 'abc.rb'
若还想在ef.rb的某个类中使用abc.rb中的模块,则应在这个类中加入
include Ma
如果你只想在ef.rb文件的某个类中使用abc.rb的模块,你得这样:
require 'abc.rb'
include Ma
这两句就告诉了你它们区别。
下面部分摘自于:http://anw.stikipad.com/ocean/show/require+load+and+include
Ruby 中 “require”, “load” 和 “include” 有甚麼不同呢? “require” 和 “load” 用途是一致的, 用來載入新的程式庫, “include” 是用來 mix-in 模組.
分享到:
相关推荐
主要介绍了Ruby中require、load、include、extend的区别介绍,require、load用于文件,如.rb等等结尾的文件,include、load则用于包含一个文件中的模块,需要的朋友可以参考下
§8.4 Require, load和include 62 第九章 异常 64 §9.1 异常处理 64 §9.2 定义异常类 68 §9.3 catch和throw 68 第十章 多任务处理 69 §10.1 多线程处理 69 §10.1.1 线程创建 69 §10.1.2 线程操作 70 §10.1.3 ...
在Ruby中加载,解析和级联CSS规则集。 建立 gem install css_parser 用法 require 'css_parser' include CssParser parser = CssParser :: Parser . new parser . load_uri! ( '...
使用这个库,您可以轻松地使用 Ruby 访问 2ch。 安装 gem install x2ch 例子 获取 2ch 类别和板列表。从服务器下载与 gzip 兼容。 require 'x2ch' include X2CH bbs = Bbs.load bbs.each{|category| puts '- ' +...
将纯文本绑定到 Ruby 类。 灵感来自 ROXML 用法 require 'structr' Load = Struct . new ( :one , :five , :fifteen ) ProcessItem = Struct . new ( :pid , :user ) class Top include Structr converter :...
Virtus::关系 将关系添加到 Virtus 对象。 安装 在您的 Gemfile 中: gem 'virtus-relations' 在您的源代码中: ... attribute :kid , Kid , relation : true , lazy : true , default : :load_k
使用以下内容创建配方: include_recipe "chef_handler"# Install `chef-handler-profiler` gem during the compile phasechef_gem "chef-handler-profiler"# load the gem here so it gets added to the $LOAD_...
在您的Rakefile ,加载aasm_graph任务文件: load "aasm/tasks/aasm_graph.rake"然后你可以运行 rake 任务: rake aasm_graph INCLUDE=./example REQUIRE=job CLASS=Job贡献分叉它( ) 创建您的功能分支( git ...