在Ruby中安全地需要宝石 [英] Safely require gems in Ruby

查看:101
本文介绍了在Ruby中安全地需要宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以安全地使用红宝石,以便在找不到宝石时不会引发异常?

Is there a way to require a ruby gem safely so as to not raise an exception if the gem is not found?

我正在寻找一种解决方案:

I am looking a solution close to this:

if require 'hirb'
  # do some hirb related stuff
else
  # do other stuff
end

我希望这样可以确保没有不必要的宝石损坏我的部署到生产。

I want this to make sure no unnecessary gems are failing my deploys to production.

推荐答案



It would probably be done like this:

begin
  require 'hirb'
rescue LoadError => e
  puts "could not find hirb"
end

这篇关于在Ruby中安全地需要宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆