读书人

ruby中require跟load的区别

发布时间: 2012-11-11 10:07:57 作者: rapoo

ruby中require和load的区别
今天在读rspec源代码的时候发现rspec会自动去load一些以_spec结尾的文件作为example 和 example group。

在这里顺便说一下ruby里loadrequire的区别。

load: 加载文件,比如load ‘example.rb’,不放重复加载 require: 加载文件,比如load ‘example’,只加载1次

代码说明:
新建2个文件。test.rb, file_to_be_load.rb
file_to_be_load.rb

puts 'It is in ' + __FILE__


test.rb

require 'file_to_be_load' # print It is in file_to_be_load.rbrequire 'file_to_be_load' # print nothingload 'file_to_be_load.rb' # print It is in file_to_be_load.rbload 'file_to_be_load.rb' # print It is in file_to_be_load.rb again


读书人网 >Ruby Rails

热点推荐