How to check the compatible Ruby versions for a gem

◇◆丶佛笑我妖孽 提交于 2020-01-11 11:29:06

问题


How do I find out if a particular gem is compatible with a certain Ruby version or not?

I want to upgrade the Ruby version of an application I am working on, But I did not see an authentic way of knowing which Ruby version is supported by a particular gem.

I found the *.gemspec of the gem, which often contains a configuration saying config.required_ruby_version ..., but I noticed not all the gems contain a gemspec file. I noticed I have a few gems on my system, ActiveRecord for example, which lack a Gemfile, whereas on GitHub, we have a gemspec file available.

This is the output of ls -lrth from my local machine:

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

The ActiveRecord repository has a gemspec file:


回答1:


Looking for the gemspec file and checking RubyGems.org is a good way to check Ruby compatibility, but as mentioned above, it would be useless if a gem author didn't write a gemspec file.

If you using a Ruby version management system like RVM or rbenv, making a simple testing script would be a solution. If Ruby version 2.4.1 and 2.5.1 have been installed on your local machine, write a script similar to "Testing With Multiple Ruby And Gem Versions" like:

#!/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 will select a Ruby version and run the test for the selected Ruby version.

If a gem doesn't have any unit tests, this test script would be useless, too. But a gem that doesn't have any unit test would not be worth using.

Although, this solution isn't an authentic way of checking for a compatible Ruby version either, it'd be worth using for your testing.




回答2:


I found something more interesting on Rubygems.org, that serves as an API that renders a JSON/XML response about the gem.

Often it is mentioned in Rubygems.org but not in the gemspec file about the Ruby version compatibility.

This is a one-off way to fetch it:

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": []
}

And extract ruby_version.

See API documentation for more information.




回答3:


I suggest looking at the gem's RubyGems page. In the left column, you will find the required Ruby version. For ActiveRecord you will need at least Ruby >= 1.9.3.

But keep in mind that this only tells you the minimum version number. Because at the time a particular version was released the developer was not able to tell if a certain future version of Ruby might introduce breaking changes.

For the max supported versions you will have to investigate the release notes or issues. In your example you will find an article "This Week in Rails: Ruby 2.4 on Rails 4.2" that Rails 4.2.8 added support for Ruby 2.4. Therefore I guess that Rails 4.2.7 only supports Ruby 2.3.



来源:https://stackoverflow.com/questions/54061319/how-to-check-the-compatible-ruby-versions-for-a-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!