没有这样的文件加载在红宝石

没有这样的文件加载在红宝石

本文介绍了如何从“需求”中拯救:没有这样的文件加载在红宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从需求中拯救:没有这样的文件以
的形式加载在ruby中,以提示用户指定-I标志,以防他忘记这样做。
基本上代码如下:

  begin 
require'someFile.rb'
rescue
放someFile.rb未找到,有没有
puts忘记指定-I标志?
退出
结束

我预计 部分接管执行,以防 someFile.rb 未找到,但我的假设是错误的。

解决方案

rescue 没有参数只能抢救StandardError s。 LoadError (由未找到的文件引发)不是StandardError ,而是 (请参阅)。因此,如MBO所示,您必须明确拯救 LoadError


I am trying to rescue from a ``require': no such file to load in ruby` in orderto hint the user at specifying the -I flag in case he has forgotten to do so.Basically the code looks like:

begin
  require 'someFile.rb'
rescue
  puts "someFile.rb was not found, have you"
  puts "forgotten to specify the -I flag?"
  exit
end

I have expected the rescue part to take over execution in case someFile.rb was not found, but my assumption was wrong.

解决方案

rescue without arguments rescues only StandardError s. The LoadError (that is raised by a file not found) is not a StandardError but a ScriptError (see http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy). Therefore you have to rescue the LoadError explicitly, as MBO indicated.

这篇关于如何从“需求”中拯救:没有这样的文件加载在红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:30