Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Rubyrescue语句修饰符是否与 一起使用require?
rescue
require
irb(main):001:0> require 'a' rescue nil LoadError: no such file to load -- a from (irb):1:in `require' from (irb):1 from :0
LoadError您可以从只需要使用begin/end样式而不使用 inline的情况中解救出来rescue:
LoadError
begin/end
这可以按您的预期工作:
begin require 'a' rescue LoadError => ex puts "Load error: #{ex.message}" end