Search code examples
rubydaterubygemsversion

Setup Ruby environment based on date or Ruby version


For testing purposes, I want to install a Ruby environment based on a past date. For instance, Ruby 2.5.0p0 came out in 2017-12-25 and somewhere I read that Rails 5.2.6 is the version to use with it. What I want is a programmatic way to know what version/date of a gem should go with what version of Ruby. Am I missing some super easy way to do this?

Edit:

My idea to test in a Ruby environment that would have existed at a certain date seems to be intractable and is possibly unnecessary. So what I will do is run through a few major Ruby releases and install their best Rails versions and let all the other gems float to whatever versions get dragged in.

Ruby    Rails
2.5.0   5.2.6
2.5.9   5.2.6
2.6.7   6.0.3.7
2.7.3   6.0.3.7
3.0.1   latest

Solution

  • What I want is a programmatic way to know what version/date of a gem should go with what version of Ruby.

    One way of doing this would be to use the API provided by RubyGems.org:

    # run with ruby get_versions.rb gemname rubyversion
    
    require 'json'
    require 'uri'
    require 'net/http'
    require 'pp'
    
    gemname = ARGV[0] || 'rails'
    uri = URI("https://rubygems.org/api/v1/versions/#{gemname}.json")
    target_ruby_version = Gem::Version.new(ARGV[1] || '2.7.0')
    
    puts "getting #{uri}"
    
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      puts '...'
      request = Net::HTTP::Get.new uri
      http.request(request)
    end
    # error handling is boring so lets just assume this will always work
    json = JSON.parse(response.body)
    compatible = json.each_with_object({}) do |gem_version, hash|
      hash[gem_version["number"]] = gem_version["ruby_version"]
    end.select do |gemv, rubyv|
      Gem::Requirement.new(rubyv)
                      .satisfied_by?(target_ruby_version)
    end
    
    puts "The following versions of #{gemname} satifisy the Ruby requirement #{target_ruby_version}:"
    
    pp compatible.keys
    

    However its actual usefulness for testing if a gem is actually compatible with a given version of Ruby is very questionable. Since authors don't usually specify a range of ruby versions it just tells you if the given Ruby version satisfies the minimum version requirement. Tom Lord does a very good job at describing why this is problematic.

    Your experience with running Rails 0.8.0 on Ruby 2.7.0 may vary.