如何在 Ruby 1.9 和 1.8 中捕获 LoadError 异常?
我有一堆需要 ftools 的 Ruby 1.8.x 脚本。
当我在 ruby 1.9 中运行这些脚本时,如何使它们停止抛出异常?
我想保留尽可能多的内容,以便脚本在 1.8 和 1.9 中都能成功运行。我不想安装 RVM 或类似的东西,因为最好只编写尽可能在 1.9 和 1.8 中运行良好的代码。
这是我已经尝试过的:
begin; require 'ftools' rescue LoadError nil end;
begin; require 'fileutils' rescue nil; end;
我怎样才能让它发挥作用。期望的结果是,如果找不到 ftools,则 ruby 会默默地失败,然后继续使用 fileutils。
I have a bunch of Ruby 1.8.x scripts that require ftools.
How can I make these scripts stop throwing exceptions when I run them in ruby 1.9?
I want to preserve as much as possible, so that the scripts run successfully in both 1.8 and 1.9. I do not want to install RVM or something like that because it would be better to just write code that runs fine in both 1.9 and 1.8, to the extent possible.
This is what I have already tried:
begin; require 'ftools' rescue LoadError nil end;
begin; require 'fileutils' rescue nil; end;
How can I get this to work. The desired outcome is for ruby to silently fail if ftools is not found, and then just go on to fileutils instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在救援处理程序中要求您的后备库?
Have you tried requiring your fallback library in the rescue handler?