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

时间:2015-11-01 22:39:59

标签: ruby linux nano

首先,我真的很擅长编码,10年前只有CMD,现在是红宝石5天。 所以我正在创建我的第一个脚本,它的唯一目标是让我测试和学习DEF和TRUE / FALSE选择

像这样:

def cls
    system "clear" or "cls"
end

def s2
    sleep 2
end

我的问题:

我希望我的脚本检查是否安装了特定的GEM,所以我制作了代码。 但我无法弄清楚如何使脚本需要安装特定的Gem IF。或者让脚本安装一个未安装的Gem IF或者至少说'#34;抱歉没有创业板,退出"然后结束脚本。

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

1 个答案:

答案 0 :(得分:1)

如果宝石存在,您可以要求宝石,如果宝石尚未按以下方式安装,也可以安装宝石(在代码中查找注释):

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