加载时显示 ruby​​ 文件的完整路径名 [英] Show full path name of the ruby file when it get loaded

查看:42
本文介绍了加载时显示 ruby​​ 文件的完整路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读源代码时,我总是想知道文件加载时的完整路径,ruby中有没有回调方法可以实现这一点,或者有其他方法可以做到这一点?提前致谢.

When reading source code, I always want to know the full path of the file when it is loaded, is there any callback method in ruby to accomplish this, or any other way to do this? thanks in advance.

编辑评论中的澄清:

我想知道在执行此行时加载的somefile"位于何处:load somefile"

I want to know where the loaded "somefile" is located while I execute this line: "load somefile"

推荐答案

我能想到的最好方法是修补 Kernel 模块.根据 docs Kernel::load$: 中搜索文件名.我们可以做同样的事情,如果找到就打印路径.

The best way I can think of is to patch the Kernel module. According to the docs Kernel::load searches $: for the filename. We can do the same thing and print the path if we find it.

module Kernel
  def load_and_print(string)
    $:.each do |p|
      if File.exists? File.join(p, string)
        puts File.join(p, string)
        break
      end
    end
    load_original(string)
  end

  alias_method :load_original, :load
  alias_method :load, :load_and_print

end

我们使用 alias_method 来存储原始的 load 方法,我们在我们的末尾调用它.

We use alias_method to store the original load method, which we call at the end of ours.

这篇关于加载时显示 ruby​​ 文件的完整路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆