ruby 如何检查是否安装了gem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22211711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:24:47  来源:igfitidea点击:

How to check if a gem is installed?

rubygem

提问by andy4thehuynh

I installed data_mapper for a Sinatra project. Curious, why is it when I do gem install brew, I can $ which brewand get the path of its location and can't for data_mapper? This works for some gems and doesn't for others.

我为 Sinatra 项目安装了 data_mapper。奇怪,为什么当我这样做时gem install brew,我可以$ which brew获取其位置的路径,而不能获取 data_mapper?这适用于某些宝石,不适用于其他宝石。

How do I verify a gem is installed properly? Would checking the version assure the gem is downloaded correctly?

如何验证 gem 是否正确安装?检查版本会确保 gem 被正确下载吗?

回答by user513951

General solution

通用解决方案

Try gem listto get the list of gems that are installed.

尝试gem list获取已安装的 gem 列表。

To test for a particular gem, you can use -iwith a regex: gem list -i "^gem_name$". (Credit to Timo in the comments for this technique.)

要测试特定的 gem,您可以使用-i正则表达式:gem list -i "^gem_name$". (在此技术的评论中归功于 Timo。)

Particular solution for OP

OP 的特殊解决方案

If you can't find data_mapper, it may be that the gem name is different from what you expect.

如果找不到data_mapper,则可能是gem名称与您期望的不同。

Also, if you're just doing which brewto find brew, you aren't finding the gem called brew, you're finding the location of the brew executable. Try gem which brewinstead.

此外,如果您只是which brew为了查找 brew,则不会找到名为 brew 的 gem,而是会找到 brew 可执行文件的位置。试试吧gem which brew

EDIT:

编辑:

If you're looking for data_mapper by doing which data_mapper, you probably won't find it. whichis a unix program for finding unix executables, and data_mapper probably doesn't have one.

如果您正在通过执行 查找 data_mapper which data_mapper,您可能找不到它。which是一个用于查找 unix 可执行文件的 unix 程序,而 data_mapper 可能没有。

Since your goal is to verify a gem is installed with the correct version, use gem list. You can limit to the specific gem by using gem list data_mapper.

由于您的目标是验证 gem 是否安装了正确的版本,因此请使用gem list. 您可以使用gem list data_mapper.

To verify that it's installed and working, you'll have to try to requirethe gem and then use it in your code.

要验证它是否已安装并正常工作,您必须尝试require使用 gem,然后在您的代码中使用它。

回答by Spajus

In case you want to use the check in a script, this gives a better output (true or false) and appropriate exit code:

如果您想在脚本中使用检查,这会提供更好的输出(真或假)和适当的退出代码:

gem list -i <gem_name>

Alternatively add the version option:

或者添加版本选项:

gem list -i <gem_name> -v version