Posted over 5 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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
about 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