如何找到安装 Ruby Gem 的路径(即 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)

查看:53
本文介绍了如何找到安装 Ruby Gem 的路径(即 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 否则它将无法工作.通常这是不可取的,因为如果您正在使用可执行 gem,您不想要求"它,否则您会收到一堆警告.

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 有一个包含所有要搜索的目录的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 的路径(即 Gem.lib_path c.f. Gem.bin_path)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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