如何找到一个Ruby宝石安装在路径(即Gem.lib_path C.F. Gem.bin_path) [英] How to find the path a Ruby Gem is installed at (i.e. Gem.lib_path c.f. Gem.bin_path)

查看:844
本文介绍了如何找到一个Ruby宝石安装在路径(即Gem.lib_path C.F. Gem.bin_path)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gem.bin_path('cucumber', 'cucumber')

将返回二进制/可执行文件的路径。
似乎没有这样的函数返回库路径。
在这种情况下将理想情况下,返回:

Will return the binary/executable's path. It seems there is no such function to return the library path. Which in this case would, ideally, return:

/home/hedge/.rvm/gems/ruby-1.9.2-p136@bbb-bdd-meta-bdd/gems/cucumber-0.10.0/lib

有我错过了什么,或有一个简单的/一个方法方式来获得这些信息?

Have I missed something or is there a simple/one method way to get this information?

更新:无CLI或非STDLIB建议,请

Updated: No CLI or non-stdlib suggestions please.

推荐答案

与核对答案的问题是,你必须规定的rubygem,否则将无法正常工作。往往是因为如果你有一个可执行的宝石的工作,你不希望规定,或者你会得到一堆警告,这是不理想的。

The problem with the checked answer is that you must "require" the rubygem or it won't work. Often this is undesireable because if you're working with an executable gem, you don't want to "require" it or you'll get a bunch of warnings.

这是可执行文件和库的通用解决方案:

This is a universal solution for executables and libs:

spec = Gem::Specification.find_by_name("cucumber")
gem_root = spec.gem_dir
gem_lib = gem_root + "/lib"

如果您想获得真正的技术,有不只是一个lib目录中。该gemspec有搜索所有directorys的require_paths阵列(添加到$ LOAD_PATH)。所以,如果你想require_paths的数组,使用:

If you want to get really technical, there isn't just one lib directory. The gemspec has a "require_paths" array of all the directorys to search (added to $LOAD_PATH). So, if you want an array of the require_paths, use this:

gem_lib = gem_root + "/" + spec.require_paths[0]

无需打捆。

这篇关于如何找到一个Ruby宝石安装在路径(即Gem.lib_path C.F. Gem.bin_path)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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