首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Rails源码阅览(四)gem_rubygems之require_Rails_require_深入理解(一)

2012-09-21 
Rails源码阅读(四)gem_rubygems之require_Rails_require_深入理解(一)Rails源码阅读(四)rubygems之require

Rails源码阅读(四)gem_rubygems之require_Rails_require_深入理解(一)

Rails源码阅读(四)rubygems之require_Rails_require_深入理解

?

提出问题:

require的行为为何不同?

require 'pp' 其实是从加载路径中找到了pp这个文件并且加载

require 'active_support' 加载了active_support这个文件,但这个文件并没有在加载路径中的,如果你自己手动加载过active_support库,你一定会知晓这个了

?

irb(main):001:0> require 'active_support'LoadError: no such file to load -- active_supportfrom (irb):1:in `require'from (irb):1

?

基本知识:require的寻找方法

?

返回值:?

? ? require(string) => 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 it's name already appears in $".

注意:

? ??However, the file name is not converted to an absolute path, so that ``require 'a';require './a''' will load a.rb twice. ? ? ?

附:

? ??require "my-library.rb" ? ? require "db-driver"

? ? require "directory1/directory2/directory3/xxx" #这样可以根据$:的相对路径寻找。

?

寻找答案:?

在1.9之前,加载gem库的话,需要先加载rubygems,也就是先执行 require 'rubygems' 这个语句。

加载前后,加载路径并没有什么变化,依然如下:

?

irb(main):005:0> pp $:["/opt/ruby/lib/ruby/site_ruby/1.8", "/opt/ruby/lib/ruby/site_ruby/1.8/i686-linux", "/opt/ruby/lib/ruby/site_ruby", "/opt/ruby/lib/ruby/vendor_ruby/1.8", "/opt/ruby/lib/ruby/vendor_ruby/1.8/i686-linux", "/opt/ruby/lib/ruby/vendor_ruby", "/opt/ruby/lib/ruby/1.8", "/opt/ruby/lib/ruby/1.8/i686-linux", "."]

但是执行了require 'activesupport'语句之后,就有变化了

?

irb(main):005:0> require 'activesupport'DEPRECATION WARNING: require "activesupport" is deprecated and will be removed in Rails 3. Use require "active_support" instead.. (called from /opt/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/activesupport.rb:2)=> trueirb(main):006:0> pp $:["/opt/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/vendor/i18n-0.1.3/lib", "/opt/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/bin", "/opt/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib", "/opt/ruby/lib/ruby/gems/1.8/gems/builder-2.1.2/bin", "/opt/ruby/lib/ruby/gems/1.8/gems/builder-2.1.2/lib", "/opt/ruby/lib/ruby/gems/1.8/gems/memcache-client-1.8.5/bin", "/opt/ruby/lib/ruby/gems/1.8/gems/memcache-client-1.8.5/lib", "/opt/ruby/lib/ruby/gems/1.8/gems/tzinfo-0.3.27/bin", "/opt/ruby/lib/ruby/gems/1.8/gems/tzinfo-0.3.27/lib", "/opt/ruby/lib/ruby/site_ruby/1.8", "/opt/ruby/lib/ruby/site_ruby/1.8/i686-linux", "/opt/ruby/lib/ruby/site_ruby", "/opt/ruby/lib/ruby/vendor_ruby/1.8", "/opt/ruby/lib/ruby/vendor_ruby/1.8/i686-linux", "/opt/ruby/lib/ruby/vendor_ruby", "/opt/ruby/lib/ruby/1.8", "/opt/ruby/lib/ruby/1.8/i686-linux", "."]

这里可以推测,require方法做了某些特殊的处理。

请留意一下,当前路径即“.”路径在最后,这个设计是多么周到,想想看,为什么~

?

查看源码

ruby的require源码,只是个stub:

?

  def require(string)    #This is a stub, used for indexing  end

?

应该仔细阅读下注释,还是比较重要的:

#1 并不是只加载.rb文件,你知道么

#2 加载次数问题和名字问题

#3 怎么防止重复加载,防止的机制是什么

?

rubygems重载了require这个最基本的方法:

?

alias gem_original_require require  def require(path) # :doc:    gem_original_require path  rescue LoadError => load_error    if load_error.message =~ /#{Regexp.escape path}\z/ and       spec = Gem.searcher.find(path) then      Gem.activate(spec.name, "= #{spec.version}")      gem_original_require path    else      raise load_error    end  end

?注释写的比较清楚,

#1 如果没有找到例如active_support,则使用Gem来activate,之后再require

#2?Gem来activate做了哪些事情呢:

? ? ?that gem is activated (added to the?loadpath).

?

? ? 所以这里改变了加载的路径

?

接下来要不要看看Gem做的事情呢。

应该的。不过可以推测一下,整体上还是做了些java世界里mvn做的一部分工作。

?

先占个位:

深入理解ruby安装,require,load,gem,等等

http://www.skorks.com/?#这个人不错阿,技术人的性格,deep into ruby!

http://www.skorks.com/2009/08/digging-into-a-ruby-installation-require-vs-load/

--deep into rails command line

http://www.skorks.com/2011/08/when-developers-go-to-great-length-to-save-typing-4-letters/

--deploy rails to tomcat 很好很强大for javaer

http://www.skorks.com/2010/02/using-ruby-on-rails-with-oracle-and-deploying-it-all-to-tomcat/

?

http://rubylearning.com/satishtalim/including_other_files_in_ruby.html#一个require和load的解释

?

====本文结束==== ----EOF---- 。。。。END。。。。

?

RoR

RoR

RoR

+o+

RoR

RoR

RoR


?

热点排行