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
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
Ruby 还允许您覆盖 require 方法。
这是一个添加基准测试以要求https://gist.github.com/278994的示例。然后您可以使用 Kernel.caller 来获取调用 require 方法的文件的名称。
不确定您是否可以知道需要哪个文件,但Module
有回调included
,extended
以便您可以对尝试包含它的另一个模块做出反应。也许您可以重构您的概念以使用此功能。
见http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-included