首先看看代码:
reqdemo.rb
puts "This is the first (master) program file."
require "requiree.rb'
require "requiree.rb'
puts "And back again to the first file."
loaddemo.rb
puts "This is the first (master) program file."
load "requiree.rb'
load "requiree.rb'
puts "And back again to the first file."
requiree.rb
puts "> This is the second file, which was 'require'd by the first."
分别运行reqdemo.rb和loaddemo.rb,结果是require只打印一次,而load打印两次
这是因为:
require load and include
Ruby中"require","load"和"include"有什么不同呢?"require"和"load"用途是一致的,用來来载入新的程序库,"include"是用来mix-in模块
"require"可载入某个a.rb文件,且可以省略".rb",而且它只会在第一次的时候载入,若再次"require"时就会忽略
require 'a'
a = A.new
"load"和"require"一样但要用 a.rb 全名, 且每次一定会重新载入
load 'a.rb'
a = A.new
载入程序库的顺序呢?类似java的class path,Ruby把这个信息存在"$:"系统全局变量上,你可以借着RUBYLIB或ruby -I来加入新的载入目录
puts $:
"include"用来mix-in某个模块,可以减少书写的长度
require 'webrick'
include WEBrick
# 可以不用 server = WEBrick::HTTPServer.new(...)
server = HTTPServer.new(...)
那么load有什么用呢?
Rails程序优先使用load而不是require来载入程序库
在development模式下,当你修改一段代码后,不用重启服务器,你的代码更改会被自动reload,这就是load的作用
而如果你使用require的话,多次require并不会起作用
对大型Ruby程序如Rails,使用多个文件和require/load/include令你的程序非常容易组织和划分模块
分享到:
相关推荐
/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require’: no such file to load — readline (LoadError) from /usr/local/lib/ruby/1.8/irb/completion.rb:10 from /usr/local/lib/ruby/1.8/irb/init...
本机支持任何形式的require和load ,包括动态需求(例如load(my_path + '/x.rb') ) Ruby Packer用Ruby编写,并使用Ruby Packer本身打包和分发 完全支持本机C扩展 开源,MIT许可 已知局限性 某些使用C扩展程序的...
dotenv 模式 Dotenv-schema 使模式化。安装编写.env和.env_schema : $ cat .envDB_HOST=db.example.comDB_PORT=3306$ cat .env_schemaDB_HOST:DB_...Sinatra 或Plain ol' Ruby $ gem install dotenv require 'dotenv-
JRuby 1.7.14 发布 ,此版本现已提供下载: ...load/require 内部重构和简化 解决了一些兼容性问题 1.7.14 修复的 bug: #1923 - String#start_with? returns false when using an empty string #1905 - T
我想与以Marshal格式存储简单值的Ruby / Rails应用程序交换数据现在我们在谈论。 我已经完成了诸如true,false和nil,Fixnums,Floats,仅包含ASCII / latin1字符,数组和哈希值的字符串之类的基础知识。 可以序列化...
i18n...# You *must* choose GetText or Rails-i18n style checking# If you want GetText-style checkingI18n/GetText: Enabled: trueI18n/RailsI18n: Enabled: false# If you want rails-i18n
与Ruby一起使用 require 'gdshowsdb' Gdshowsdb . init ( ) Gdshowsdb . load ( ) jack_straw_shows = SongRef . find_by_name ( 'Jack Straw' ) . shows jack_straw_shows . each do | show | puts show . title ...
引导规模 加速应用程序通过在需求调用期间缓存文件... initializer :regenerate_require_cache , before : :load_environment_config do Bootscale . regenerate end end end Rails应用 在config/boot.rb找到confi
允许您在非Rails项目中使用ActiveRecord迁移。 安装 将此行添加到应用程序的Gemfile中(如果没有,请运行bundle init ): gem 'active_record_migrations' gem 'sqlite3' # or 'pg', 'mysql2', ... 然后执行: ...
一个快速的JSON解析器和Object marshaller作为Ruby gem。 3.0版已发布! 3.0提供了更好的json gem和Rails兼容性。 它还提供了其他优化选项。 使用 require 'oj' h = { 'one' => 1 , 'array' => [ true , false ] }...
fetch ( 'PUSHER_API_KEY' ) 一旦您可以从前端访问它,您就可以在您的 Opal 应用程序中打开一个 Pusher 连接,如下所示: require 'pusher' # Load this librarypusher = Pusher . new ( `gon.push
单冠 可行的代码覆盖率。 rspec spec/foobar_spec.rb ...... 114 example, 0 failures lib/foobar.rb new uncovered lines ... load before loading rails / minitest / libraries require 'single_cov' SingleC