[英]How to check if a gem is installed?
I installed data_mapper for a Sinatra project.我为 Sinatra 项目安装了 data_mapper。 Curious, why is it when I do gem install brew
, I can $ which brew
and get the path of its location and can't for data_mapper?奇怪,为什么当我执行gem install brew
,我可以$ which brew
并获取其位置的路径而不能用于 data_mapper? This works for some gems and doesn't for others.这适用于某些宝石,不适用于其他宝石。
How do I verify a gem is installed properly?如何验证 gem 是否正确安装? Would checking the version assure the gem is downloaded correctly?检查版本会确保 gem 被正确下载吗?
General solution通用解决方案
To get the full list of gems that are installed:要获取已安装的 gem 的完整列表:
gem list
To test for a particular gem, you can use -i
with a regex:要测试特定的 gem,您可以将-i
与正则表达式一起使用:
gem list -i "^gem_name$"
(Credit to Timo in the comments for this technique.) (在此技术的评论中归功于 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 brew
to find brew, you aren't finding the gem called brew, you're finding the location of the brew executable.此外,如果您只是在执行which brew
来查找 brew,那么您不会找到名为 brew 的 gem,而是会找到 brew 可执行文件的位置。 Try gem which brew
instead.试试gem which brew
代替。
EDIT:编辑:
If you're looking for data_mapper by doing which data_mapper
, you probably won't find it.如果您通过执行which data_mapper
来查找which data_mapper
,您可能找不到它。 which
is a unix program for finding unix executables, and data_mapper probably doesn't have one. which
是一个用于查找 unix 可执行文件的 unix 程序,而 data_mapper 可能没有。
Since your goal is to verify a gem is installed with the correct version, use gem list
.由于您的目标是验证 gem 是否安装了正确的版本,因此请使用gem list
。 You can limit to the specific gem by using gem list data_mapper
.您可以使用gem list data_mapper
来限制特定的 gem。
To verify that it's installed and working , you'll have to try to require
the gem and then use it in your code.要验证它是否已安装并正常工作,您必须尝试require
gem,然后在您的代码中使用它。
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
如果您在项目中使用bundle
,则使用以下命令检查项目中您的 gem 是否完全匹配。
bundle info ^data_mapper$
对于脚本使用, gem query
可能更好:
gem query --silent --installed --exact rubygems --version 2.0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.