代码之家  ›  专栏  ›  技术社区  ›  miku

python在ruby中的uuu文件的等价物是什么?

  •  1
  • miku  · 技术社区  · 14 年前

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

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

    >> require 'xmlrpc/client'
    => true
    >> ...
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Jörg W Mittag    14 年前

    没有完全相同的。

    所有已删除的文件 required 列在 $LOADED_FEATURES require d。所以,如果你想知道一个文件是从哪里来的 要求 d、 你只需要看看结尾:

    $LOADED_FEATURES.last if require 'yaml'
    # => 'C:/Program Files/Ruby/lib/ruby/1.9.1/yaml.rb'
    

    很难找出哪个条目对应哪个调用。另外,如果文件已经在 ,则不会再次加载:

    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 'yaml' 不仅仅是负载 yaml.rb

    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
  •   BaroqueBobcat    14 年前

    假设您使用的是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 从命令行。

        3
  •  1
  •   John Feminella    14 年前

    没有完全匹配的。不过,你自己很容易找到:

    # 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.