How to check the compatible Ruby versions for a gem

后端 未结 3 1408
生来不讨喜
生来不讨喜 2021-01-25 08:58

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 n

相关标签:
3条回答
  • 2021-01-25 09:19

    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.

    0 讨论(0)
  • 2021-01-25 09:25

    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.

    0 讨论(0)
  • 2021-01-25 09:36

    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.

    0 讨论(0)
提交回复
热议问题