Obromios
Obromios

Reputation: 16373

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?

Upvotes: 1

Views: 462

Answers (2)

lobati
lobati

Reputation: 10215

According to the docs running the file will install the dependencies. Without a lock file being generated, though, you might want to manually specify versions in your gemfile block:

gemfile do
  source 'https://rubygems.org'
  ruby '2.7.3'
  gem 'colorize', '~> 0.8.1'
  gem 'pry', '~> 0.14.1'
end

Your script likely won't be compatible with all versions of a library, so it's probably a good idea to add version constraints when there's no lock file. Then when you want to upgrade, you update the number and Bundler should install the new version.

Upvotes: 1

Eric Terry
Eric Terry

Reputation: 364

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.

Upvotes: 1

Related Questions