Search code examples
rubybundler

How to update a gem in a ruby script


I have a ruby script with the requisite gems specified within it e.g

#!/usr/bin/env ruby
require 'bundler/inline'
require 'matrix'
gemfile do
  source 'https://rubygems.org'
  ruby '2.7.3'
  gem 'colorize'
  gem 'pry'
end

puts "warning".colorize(:red)

Normally to update a gem, I would type something like bundle update colorize, but this returns an error

Could not locate Gemfile 

So how do I update a gem in this script. Is there an equivalent of a Gemfile.lock that I can list?


Solution

  • Because using bundler in a single-file ruby script uses the latest constrained gem installed, in order to update one of the gems, you just have to run this (according to your example)

    gem update colorize
    

    Now your script will use the latest colorize gem version.