0

I would like to be able to give a required file the knowledge of the requiring, like so

#requiring file
require 'required_file', __FILE__

or

#required file
puts __REQUIRING_FILE__

is there a way to achieve that somehow ? Thanks

4

2 回答 2

0

Ruby 还允许您覆盖 require 方法。

这是一个添加基准测试以要求https://gist.github.com/278994的示例。然后您可以使用 Kernel.caller 来获取调用 require 方法的文件的名称。

于 2012-08-09T16:40:02.457 回答
0

不确定您是否可以知道需要哪个文件,但Module有回调includedextended以便您可以对尝试包含它的另一个模块做出反应。也许您可以重构您的概念以使用此功能。

http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-included

于 2012-08-09T15:07:30.300 回答