在ruby中,python的__file__相当于什么?

时间:2010-06-21 15:21:20

标签: python ruby path module

在导入后的python中,可以看到已加载的文件/模块来自的位置。

>>> import os
>>> os.__file__
'/Users/tm/lib/python2.6/os.pyc'

红宝石中的等价物是什么?

>> require 'xmlrpc/client'
=> true
>> ...

3 个答案:

答案 0 :(得分:1)

没有什么是完全匹配的。不过,很容易找到它自己:

# 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/ruby-1.9.1-p378@standard/gems/haml-3.0.12/bin" 
# --> Here's a third-party library from my gems.

答案 1 :(得分:1)

没有什么完全等同。

所有required的文件都按$LOADED_FEATURES的顺序列在require中。所以,如果您想知道文件在require之后直接来自何处,您只需要查看结尾:

$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个):

  1. C:/Program Files/Ruby/lib/ruby/1.9.1/i386-mingw32/stringio.so
  2. C:/Program Files/Ruby/lib/ruby/1.9.1/i386-mingw32/syck.so
  3. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/error.rb
  4. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/basenode.rb
  5. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/syck.rb
  6. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/tag.rb
  7. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/stream.rb
  8. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/constants.rb
  9. C:/Program Files/Ruby/lib/ruby/1.9.1/date/format.rb
  10. C:/Program Files/Ruby/lib/ruby/1.9.1/date.rb
  11. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/rubytypes.rb
  12. C:/Program Files/Ruby/lib/ruby/1.9.1/syck/types.rb
  13. C:/Program Files/Ruby/lib/ruby/1.9.1/yaml/syck.rb
  14. C:/Program Files/Ruby/lib/ruby/1.9.1/syck.rb
  15. C:/Program Files/Ruby/lib/ruby/1.9.1/yaml.rb

答案 2 :(得分:1)

假设您使用的是rubygems,您可以使用Gem.find_files(file)找出它加载的文件。

e.g:

>> 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'加载的文件。

找出require加载哪个文件的另一种方法是从命令行调用$ gem which foo