0

I am dealing with a very minor issue here. I am simply trying to require some gems, and just simply revert to the code in the rescue block if the gems aren't installed. However, it's never reaching the rescue even though it's in a begin/rescue block:

begin
  ['net/http', 'getopt/std', 'pry','json'].each(&method(:require))
rescue
  puts "A gem required for this script is missing. It is installing now."
  `gem install getopt`
  ['net/http', 'getopt/std'].each(&method(:require))
end

When I run this script on a system that doesn't have the first gem, net/http installed, it gives me the following error:

Traceback (most recent call last):
    2: from ./agent.rb:16:in `<main>'
    1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- getopt/std (LoadError)

Shouldn't this fall into the rescue block? What am I doing wrong that's resulting in it not falling into the rescue block?

LewlSauce
  • 5,326
  • 8
  • 44
  • 91

1 Answers1

0

Found the answer here: How to rescue from a require "gem_name" when the gem is not installed

Just have to change rescue to rescue LoadError and that solves this problem. For whatever reason, I thought including just rescue without anything after it would catch all.

LewlSauce
  • 5,326
  • 8
  • 44
  • 91