Posted about 6 years ago. Visible to the public.

A Ruby script that installs all gems it is missing

So you want your Ruby script to install missing gems instead of dying? Take this method:

Copy
def installing_missing_gems(&block) yield rescue LoadError => e gem_name = e.message.split('--').last.strip install_command = 'gem install ' + gem_name # install missing gem puts 'Probably missing gem: ' + gem_name print 'Auto-install it? [yN] ' gets.strip =~ /y/i or exit(1) system(install_command) or exit(1) # retry Gem.clear_paths puts 'Trying again ...' require gem_name retry end

Use it like this:

Copy
installing_missing_gems do require 'thor' require 'nokogiri' # some code using those gems end

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 5 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more