如何从 gem 内部引用文件

How to reference a file from inside of a gem

从 gem 中查找文件绝对路径的正确方法是什么?换句话说,假设我在 gem_install_path/mygem/lib/mygem.rb 中,我想要通往 gem_install_path/mygem/foo/ 的路径。获得它的正确方法是什么?

不清楚您想要哪条路径,但可能以下其中一项可以满足您的需求:

Gem.loaded_specs["mygem"].base_dir
Gem.loaded_specs["mygem"].full_gem_path
Gem.loaded_specs["mygem"].gem_dir
Gem.loaded_specs["mygem"].gems_dir