如何检查gem适用的Ruby版本

3
我要如何确定某个 gem 是否兼容特定的 Ruby 版本?
我想要升级正在开发中的应用程序的 Ruby 版本,但是我找不到一个可靠的方法来知道哪个 Ruby 版本被支持。我发现 gem 的 `*.gemspec` 文件通常包含配置信息,其中会声明 `config.required_ruby_version ...`,但并非所有的 gem 都包含 gemspec 文件。例如,我在系统上有一些 gem(例如 ActiveRecord)缺少 Gemfile 文件,而在 GitHub 上,我们可以找到 gemspec 文件。
下面是从我的本地机器输出的 `ls -lrth` 命令结果:
Einstiens-MacBook-Pro:activerecord-4.2.7.1 superhero$ ls -lrth
total 128
-rw-r--r--  1 superhero  jingle    51K Dec 23 00:07 CHANGELOG.md
-rw-r--r--  1 superhero  jingle   1.0K Dec 23 00:07 MIT-LICENSE
-rw-r--r--  1 superhero  jingle   6.6K Dec 23 00:07 README.rdoc
drwxr-xr-x  4 superhero  jingle   128B Dec 23 00:07 examples
drwxr-xr-x  5 superhero  jingle   160B Dec 23 00:07 lib

ActiveRecord仓库中有一个gemspec文件: enter image description here

3个回答

2
寻找gemspec文件并检查RubyGems.org是检查Ruby兼容性的好方法,但正如上面所提到的,如果gem作者没有编写gemspec文件,则这种方法将无效。
如果您使用像RVM或rbenv这样的Ruby版本管理系统,编写一个简单的测试脚本可能是一种解决方案。如果您的本地计算机已安装了Ruby版本2.4.1和2.5.1,请编写类似于“Testing With Multiple Ruby And Gem Versions”的脚本:
#!/usr/bin/env bash

set -e

rubies=("ruby-2.4.1" "ruby-2.5.3")
for i in "${rubies[@]}"
do
  echo "====================================================="
  echo "$i: Start Test"
  echo "====================================================="
  rvm $i exec bundle
  rvm $i exec bundle exec rake test
  echo "====================================================="
  echo "$i: End Test"
  echo "====================================================="
done

RVM会选择一个Ruby版本并运行所选的Ruby版本的测试。

如果一个gem没有任何单元测试,这个测试脚本也将是无用的。但是一个没有任何单元测试的gem是不值得使用的。

虽然这种解决方案并不是检查兼容的Ruby版本的真正方法,但对于您的测试而言仍然值得使用。


0

我建议查看宝石的RubyGems页面。在左侧栏中,您将找到所需的Ruby版本。对于ActiveRecord,您至少需要Ruby >= 1.9.3

但请记住,这只告诉您最低版本号。因为在发布特定版本时,开发人员无法确定某个未来版本的Ruby是否会引入破坏性更改。

对于最大支持版本,您必须调查发布说明或问题。在您的示例中,您将找到一篇文章“This Week in Rails: Ruby 2.4 on Rails 4.2”,其中Rails 4.2.8添加了对Ruby 2.4的支持。因此,我猜测Rails 4.2.7仅支持Ruby 2.3。


这与我对升级挑战的思考相一致,我认为测试覆盖率是安全的做法… - Nishutosh Sharma

0

我在Rubygems.org上发现了一些更有趣的东西,它作为一个API提供了有关gem的JSON/XML响应。

通常在Rubygems.org中提到,但在gemspec文件中没有提及Ruby版本的兼容性。

这是一种获取它的一次性方法:

curl https://rubygems.org/api/v2/rubygems/activerecord/versions/4.2.7.1.json

{
  "name": "activerecord",
  "downloads": 163190934,
  "version": "4.2.7.1",
  "version_downloads": 6061660,
  "platform": "ruby",
  "authors": "David Heinemeier Hansson",
  "info": "Databases on Rails. Build a persistent domain model by mapping database tables to Ruby classes. Strong conventions for associations, validations, aggregations, migrations, and testing come baked-in.",
  "licenses": [
    "MIT"
  ],
  "metadata": {},
  "sha": "923a64e2ebb9c4529761bf65ed37601a7000af2f3b18f12ea00e9f9ba2a168d2",
  "project_uri": "https://rubygems.org/gems/activerecord",
  "gem_uri": "https://rubygems.org/gems/activerecord-4.2.7.1.gem",
  "homepage_uri": "http://rubyonrails.org",
  "wiki_uri": null,
  "documentation_uri": "http://www.rubydoc.info/gems/activerecord/4.2.7.1",
  "mailing_list_uri": null,
  "source_code_uri": null,
  "bug_tracker_uri": null,
  "changelog_uri": null,
  "dependencies": {
    "development": [],
    "runtime": [
      {
        "name": "activemodel",
        "requirements": "= 4.2.7.1"
      },
      {
        "name": "activesupport",
        "requirements": "= 4.2.7.1"
      },
      {
        "name": "arel",
        "requirements": "~> 6.0"
      }
    ]
  },
  "built_at": "2016-08-10T00:00:00.000Z",
  "created_at": "2016-08-11T17:33:45.486Z",
  "description": "Databases on Rails. Build a persistent domain model by mapping database tables to Ruby classes. Strong conventions for associations, validations, aggregations, migrations, and testing come baked-in.",
  "downloads_count": 6061660,
  "number": "4.2.7.1",
  "summary": "Object-relational mapper framework (part of Rails).",
  "rubygems_version": ">= 0",
  "ruby_version": ">= 1.9.3",
  "prerelease": false,
  "requirements": []
}

并提取ruby_version

有关更多信息,请参见{{link1:API文档}}。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接