Skip to content
Advertisement

Ruby: How to REQUIRE an installed GEM programmatically?

First off, I am REALLY new at coding, only CMD 10 years ago and ruby for 5 days now. So I am making my very first script and the only goal for it is for me to test and learn DEF and TRUE/FALSE choices

Like like this:

def cls
    system "clear" or "cls"
end

or

def s2
    sleep 2
end

My question:

I want my script to check if a particular GEM is installed, so i made the code. but i cant figure out how to make the script REQUIRE the particular Gem IF installed. or make the script INSTALL a Gem IF not installed OR at least say “sorry no GEM, EXIT” and then end script.

def checkGemColorize(gemName, versionLimit=nil)
isAvailable = false
begin
    if versionLimit == nil
        gem  gemName
       puts "Yes the GEM is installed"
#the script should put in require 'colorize' if installed
   else
     gem  gemName, versionLimit
       puts "Yes the GEM is installed with correct version number"
#also if version number is used.
    end
    isAvailable = true
rescue LoadError
end
isAvailable
end
sleep 0.5
#testing IF TRUE on colorize
puts "checking for colorize GEM"
  sleep 0.5
puts checkGemColorize('colorize')
  sleep 1
#testing IF TRUE version number
puts "checking for colorize version number"
  sleep 0.5
puts checkGemColorize('colorize', '>=0.6')
  sleep 1
#testing IF FALSE version number is shown when input is higher
puts "checking false version number"
  sleep 0.5
puts checkGemColorize('colorize', '>=1.2')
  sleep 1
#testing IF FALSE parameter
puts "a fake gem for testing FALSE"
  sleep 0.5
puts checkGemColorize('not_colorize')
  sleep 1
puts "testing"
  sleep 1
puts "test".red

This is the working code thanks to K M Rakibul Islam

def checkGemColorize(gemName, versionLimit=nil)
  isAvailable = false
  begin
    if versionLimit == nil
      gem_present = gem  gemName # this will return true if the gem is present
      if gem_present
        puts "Yes the GEM is installed"
        require gemName # here you are requiring the gem
        puts "#{gemName} GEM is required just now"
      end
    else
      gem  gemName, versionLimit
      puts "Yes the GEM is installed with correct version number"
#also if version number is used.
    end
    isAvailable = true
  rescue LoadError
    # I added this block of code to install the gem when it's missing
    puts "#{gemName} is missing, Installing now...."
    `gem install #{gemName}` # installing the missing gem
    puts "installed the #{gemName} gem just now!"
    isAvailable = true
  end
  isAvailable
end

Advertisement

Answer

You can require the gem if it’s present and also install the gem if it’s not already installed the following way (Look for the comments in the code):

def checkGemColorize(gemName, versionLimit=nil)
  isAvailable = false
  begin
    if versionLimit == nil
      gem_present = gem  gemName # this will return true if the gem is present
      if gem_present
        puts "Yes the GEM is installed"
        require gemName # here you are requiring the gem
        puts "#{gemName} GEM is required just now"
      end
    else
      gem  gemName, versionLimit
      puts "Yes the GEM is installed with correct version number"
#also if version number is used.
    end
    isAvailable = true
  rescue LoadError
    # I added this block of code to install the gem when it's missing
    puts "#{gemName} is missing, Installing now...."
    `gem install #{gemName}` # installing the missing gem
    puts "installed the #{gemName} gem just now!"
    isAvailable = true
  end
  isAvailable
end

#testing IF TRUE on colorize
puts "checking for colorize GEM"
puts checkGemColorize('colorize')
Advertisement