获取无安装的红宝石宝石版本
如果我有一个本地的宝石(gemfile.gem),如何在不安装宝石的情况下从代码或命令行中获取名称和版本信息。
原因:我正在安装用户宝石来验证它,并希望将其卸载以清理。用户宝石不是我控制的东西,因此我不能依靠命名约定。
CLI解决方案:
gem spec gemfile.gem name
gem spec gemfile.gem version
Ruby解决方案:
name = Psych.safe_load(`gem spec gemfile.gem name`).to_s
version = Psych.safe_load(`gem spec gemfile.gem version`, permitted_classes: [Gem::Version]).to_s
# Now you can uninstall the gem with
Gem::Uninstaller.new(name, {:version => version, :force => true}).uninstall
If I have a local gem (gemfile.gem) how can I get the name and version information from code or the command line without installing the gem.
Reason: I'm installing a user gem to validate it and want to uninstall it to clean up. User gem is not something I control so I can't depend on naming conventions.
CLI Solution:
gem spec gemfile.gem name
gem spec gemfile.gem version
Ruby Solution:
name = Psych.safe_load(`gem spec gemfile.gem name`).to_s
version = Psych.safe_load(`gem spec gemfile.gem version`, permitted_classes: [Gem::Version]).to_s
# Now you can uninstall the gem with
Gem::Uninstaller.new(name, {:version => version, :force => true}).uninstall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
gemfile.lock
其他选项中看到锁定版本,但需要
bundle install
首先更新:
如果您需要检查本地GEM版本,例如
SOONE GEM 。来自二进制的信息
,或者
您也可以使用Ruby格式来查看它,
当然您可以使用
grep
用版本
Word过滤行,但是最好以这样的参数将其传递给
You can see locked version in
Gemfile.lock
Other option, but need
bundle install
firstUpdate:
If you need to check local gem version, for example
some-gem.gem
, you can use such command to parse all information from binaryor just
You can also look it with Ruby format
Of course you can use
grep
to filter lines withversion
wordBut it's better to pass it as argument like this
您的问题模棱两可。如果您的意思是“我该如何阅读Gemspec的宝石名称和版本?”然后,您可以使用 gem ::规格#加载。例如,假设您有一个具有标准布局的宝石,并且
foo_bar.gemspec
在宝石项目目录的根部中,您可以使用git来查找项目的顶级级别,并在Gemspec中阅读:然后,您可以用SED,尴尬或切割来解析输出。
Your question is ambiguous. If you mean "How can I read in the gem name and version from the gemspec?" then you can use the output of Gem::Specification#load. For example, assuming you have a gem with a standard layout and
foo_bar.gemspec
in the root of your gem's project directory, you can use Git to find the top-level of your project and read in the gemspec:You can then parse the output with sed, awk, or cut.
gemfile
没有(一定)指定依赖关系的确切版本。它可能没有指定(即“任何版本不错”)或〜> 1.0
(即> = 1.0
and< 2.0
)或其他。同样,依赖性约束可能会进一步限制版本的有效范围。我假设这不是您的问题的意思。相反,您想知道依赖关系的确切版本将通过运行
bundle install
,给定gemfile.lock.lock
来安装。 。通过解析来解析
gemfile.lock.lock
::A
Gemfile
does not (necessarily) specify an exact version of a dependency. It might specify nothing (i.e. "any version is fine"), or~> 1.0
(i.e.>= 1.0
and< 2.0
), or whatever. Also, dependency constraints might further restrict the valid range of versions.I'm assuming that this isn't what you meant by your question. Instead, you'd like to know what exact versions of dependencies will be installed by running
bundle install
, given aGemfile.lock
.One way to achieve this reliably (i.e. rather than using
grep
and eyeballing which line(s) are most relevant) is by parsing theGemfile.lock
: