发现 ruby​​ require 方法会加载文件?

发布于 2024-09-12 15:54:00 字数 152 浏览 5 评论 0原文

ruby 中的 require 方法将搜索 lib_path 并加载找到的第一个匹配文件(如果需要)。无论如何,是否可以打印要加载的文件的路径。我正在寻找类似于 bash 中的 which 命令的理想内置功能,并希望它也能这么简单。谢谢。

The require method in ruby will search the lib_path and load the first matching files found if needed. Is there anyway to print the path to the file which would be loaded. I'm looking for, ideally built-in, functionality similar to the which command in bash and hoping it can be that simple too. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-09-19 15:54:00

我不知道内置功能,但定义自己的功能并不难。这是改编自这个问题<的解决方案< /a>:

def which(string)
  $:.each do |p|
    if File.exist? File.join(p, string)
      puts File.join(p, string)
      break
    end
  end
end

which 'nokogiri'
#=> /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri

说明:$: 是预定义变量。它是一个数组,用于搜索可以 loadrequire 的文件。 which 方法遍历每个路径,查找您调用它的文件。如果找到匹配项,则返回文件路径。

我假设您只希望输出为一行,显示所需文件的完整文件路径,例如 which。如果您还想查看您的 required 文件将自行加载的文件,则链接问题中的解决方案可能更合适:

module Kernel
  def require_and_print(string)
    $:.each do |p|
      if File.exist? File.join(p, string)
        puts File.join(p, string)
        break
      end
    end
    require_original(string)
  end

  alias_method :require_original, :require
  alias_method :require, :require_and_print

end

require 'nokogiri'
#=>  /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rbconfig
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/pp
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/sax
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/xpath
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xslt
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/html
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/css
#    /opt/local/lib/ruby1.9/1.9.1/racc/parser.rb  

I don't know of a built-in functionality, but defining your own isn't hard. Here's a solution adapted from this question:

def which(string)
  $:.each do |p|
    if File.exist? File.join(p, string)
      puts File.join(p, string)
      break
    end
  end
end

which 'nokogiri'
#=> /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri

Explanation: $: is a pre-defined variable. It's an array of places to search for files you can load or require. The which method iterates through each path looking for the file you called it on. If it finds a match, it returns the file path.

I'm assuming you just want the output to be a single line showing the full filepath of the required file, like which. If you want to also see the files your required file will load itself, something like the solution in the linked question might be more appropriate:

module Kernel
  def require_and_print(string)
    $:.each do |p|
      if File.exist? File.join(p, string)
        puts File.join(p, string)
        break
      end
    end
    require_original(string)
  end

  alias_method :require_original, :require
  alias_method :require, :require_and_print

end

require 'nokogiri'
#=>  /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rbconfig
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/pp
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/sax
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/xpath
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xslt
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/html
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/css
#    /opt/local/lib/ruby1.9/1.9.1/racc/parser.rb  
囍笑 2024-09-19 15:54:00

$ gem 其文件名 #(无 .rb 后缀)是我使用的...

$ gem which filename # (no .rb suffix) is what I use...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文