开发者

How can I trap LoadError exceptions in Ruby 1.9 and 1.8?

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.


Have you tried requiring your fallback library in the rescue handler?

begin
  require 'ftools'
rescue LoadError
  require 'fileutils'
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜