在python導入後,可以看到已加載的文件/模塊的來源。在ruby中,python的__file__等價於什麼?
>>> import os
>>> os.__file__
'/Users/tm/lib/python2.6/os.pyc'
在紅寶石中有什麼等價物?
>> require 'xmlrpc/client'
=> true
>> ...
在python導入後,可以看到已加載的文件/模塊的來源。在ruby中,python的__file__等價於什麼?
>>> import os
>>> os.__file__
'/Users/tm/lib/python2.6/os.pyc'
在紅寶石中有什麼等價物?
>> require 'xmlrpc/client'
=> true
>> ...
沒有什麼完全相同的。
required
的所有文件在$LOADED_FEATURES
中按照他們的順序排列爲require
d。所以,如果你想知道一個文件直接後,這是require
d來了,你只需要在最後一下:
$LOADED_FEATURES.last if require 'yaml'
# => 'C:/Program Files/Ruby/lib/ruby/1.9.1/yaml.rb'
但是,除非你記錄每次調用require
這將是很難找出哪個條目對應哪個呼叫。另外,如果一個文件已在$LOADED_FEATURES
,它不會再次加載:
require 'yaml'
# => true
# true means: the file was loaded
$LOADED_FEATURES.last
# => 'C:/Program Files/Ruby/lib/ruby/1.9.1/yaml.rb'
require 'json'
$LOADED_FEATURES.last
# => 'C:/Program Files/Ruby/lib/ruby/1.9.1/json.rb'
require 'yaml'
# => false
# false means: the file wasn't loaded again, because it has already been loaded
$LOADED_FEATURES.last
# => 'C:/Program Files/Ruby/lib/ruby/1.9.1/json.rb'
# Last loaded feature is still JSON, because YAML wasn't actually loaded twice
另外,許多庫不包含在一個單一的文件。因此,require
d文件本身可能包含對require
的調用。在我的情況下,例如,require 'yaml'
不僅加載yaml.rb
但文件一大堆(15確切):
C:/Program Files/Ruby/lib/ruby/1.9.1/i386-mingw32/stringio.so
C:/Program Files/Ruby/lib/ruby/1.9.1/i386-mingw32/syck.so
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/error.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/basenode.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/syck.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/tag.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/stream.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/constants.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/date/format.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/date.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/rubytypes.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck/types.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/yaml/syck.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/syck.rb
C:/Program Files/Ruby/lib/ruby/1.9.1/yaml.rb
沒有什麼是完全匹配的。但是,您可以很容易地找到它:
# Find where a path `p` was loaded from.
def locate(p)
# Find the first path in your load-paths that contains a file matching `p`.
$:.find { |l|
File.exists?(File.join(l, p))
}
end
ruby-1.9.1-p378 > locate('yaml')
=> "/home/johnf/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1"
# --> This tells you that 'yaml.rb' was loaded from here.
ruby-1.9.1-p378 > locate('zzz')
=> nil
# --> There's no matches for this library.
ruby-1.9.1-p378 > locate('haml')
=> "/home/johnf/.rvm/gems/[email protected]/gems/haml-3.0.12/bin"
# --> Here's a third-party library from my gems.
假設你正在使用rubygems,你可以通過使用Gem.find_files(file)
找出它加載哪個文件。
例如爲:
>> puts Gem.find_files('active_record')
/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0.beta2/lib/active_record.rb
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record.rb
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record.rb
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record.rb
數組的第一元素是由require 'active_record'
加載的文件。
找出哪個文件按要求加載的另一種方法是從命令行調用$ gem which foo
。