Ruby:如何以编程方式要求安装 GEM?

Ruby: How to REQUIRE an installed GEM programmatically?

首先,我真的是编码新手,10 年前只有 CMD,现在 ruby 才 5 天。 所以我正在制作我的第一个脚本,唯一的目标是测试和学习 DEF 和 TRUE/FALSE 选择

像这样:

def cls
    system "clear" or "cls"
end

def s2
    sleep 2
end

我的问题:

我想让我的脚本检查是否安装了特定的 GEM,所以我编写了代码。 但我无法弄清楚如何使脚本需要特定的 Gem IF 安装。或使脚本安装 Gem 如果未安装或至少说 "sorry no GEM, EXIT" 然后结束脚本。

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

这是工作代码,感谢 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

如果 gem 存在,您可以要求它,如果还没有安装 gem,则可以通过以下方式安装(查找代码中的注释):

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')