谁在给我的gem打电话?
Who's calling my gem?
我正在写一个 gem,在我的 gem 中,我需要知道调用我的 gem 的文件的位置(绝对)。我怎样才能得到这些信息?我试过使用与 caller
相关的东西,但它没有给出位置,只有调用方方法名称。
# requieree.rb
puts File.absolute_path(caller.first.split(':').first)
#requierer.rb
require_relative 'requieree'
ruby requierer.rb # => /home/__user__/Desktop/requierer.rb
caller
是过时的。使用 caller_locatons
.
caller_locations(0, 1).first.absolute_path
如果这不是您想要的,请将 caller_locations
的第一个参数更改为 1
,等等
我正在写一个 gem,在我的 gem 中,我需要知道调用我的 gem 的文件的位置(绝对)。我怎样才能得到这些信息?我试过使用与 caller
相关的东西,但它没有给出位置,只有调用方方法名称。
# requieree.rb
puts File.absolute_path(caller.first.split(':').first)
#requierer.rb
require_relative 'requieree'
ruby requierer.rb # => /home/__user__/Desktop/requierer.rb
caller
是过时的。使用 caller_locatons
.
caller_locations(0, 1).first.absolute_path
如果这不是您想要的,请将 caller_locations
的第一个参数更改为 1
,等等