Ruby 中的 fail 关键字有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18811675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the fail keyword do in Ruby?
提问by Dreams
I am learning Ruby and encountered the failkeyword. What does it mean?
我正在学习 Ruby 并遇到了fail关键字。这是什么意思?
if password.length < 8
fail "Password too short"
end
unless username
fail "No user name set"
end
回答by crazybob
In Ruby, failis synonymous with raise. The failkeyword is a method of the Kernelmodule which is included by the class Object. The failmethod raises a runtime error just like the raisekeyword.
在 Ruby 中,fail是raise. 该fail关键字是所述的方法Kernel,其是由类包括模块Object。该fail方法会引发运行时错误,就像raise关键字一样。
The failmethod has three overloads:
该fail方法具有三个重载:
fail: raises aRuntimeErrorwithout an error message.fail(string): raises aRuntimeErrorwith the string argument as an error message:fail "Failed to open file"fail(exception [, string [, array]]): raises an exception of classexception(first argument) with an optional error message (second argument) and callback information (third argument).Example: Assume you define a function which should fail if given a bad argument. It is better to raise an
ArgumentErrorand not aRuntimeError:fail ArgumentError, "Illegal String"Another Example: You can pass the whole backtraceto the
failmethod so you can access the trace inside therescueblock:fail ArgumentError, "Illegal String", callercalleris a Kernel method which returns the backtrace as an array of strings in the formfile:line: in 'method'.
fail: 引发一个RuntimeError没有错误消息。fail(string): 引发RuntimeError带有字符串参数的 a 作为错误消息:fail "Failed to open file"fail(exception [, string [, array]]): 引发类exception(第一个参数)的异常,带有可选的错误消息(第二个参数)和回调信息(第三个参数)。示例:假设您定义了一个函数,如果给出一个错误的参数,该函数应该会失败。最好提出 an
ArgumentError而不是 aRuntimeError:fail ArgumentError, "Illegal String"另一个示例:您可以将整个回溯传递给该
fail方法,以便您可以访问rescue块内的跟踪:fail ArgumentError, "Illegal String", callercaller是一个内核方法,它将回溯作为表单中的字符串数组返回file:line: in 'method'。
With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, raises a RuntimeError with the string as a message. Otherwise, the first parameter should be the name of an Exception class (or an object that returns an Exception object when sent an exception message). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end blocks.
没有参数,在 $! 中引发异常。或者如果 $! 为零。使用单个 String 参数,以字符串作为消息引发 RuntimeError。否则,第一个参数应该是 Exception 类的名称(或在发送异常消息时返回 Exception 对象的对象)。可选的第二个参数设置与异常关联的消息,第三个参数是回调信息数组。异常由 begin...end 块的救援子句捕获。
Source: Ruby Documentation on the Kernel Module.
来源:关于内核模块的 Ruby 文档。
回答by kuboon
Rubocopsays about usage of both words;
Rubocop谈到了这两个词的用法;
'Use
failinstead ofraiseto signal exceptions.''Use
raiseinstead offailto rethrow exceptions.'
'使用
fail而不是raise发出异常信号。''使用
raise而不是fail重新抛出异常。
Here is an example.
这是一个例子。
def sample
fail 'something wrong' unless success?
rescue => e
logger.error e
raise
end
回答by Boris Stitnicky
fail== raise
fail== raise
In other words, failis just a popular alias for raiseerror-raising method. Usage:
换句话说,fail只是raise错误引发方法的流行别名。用法:
fail ArgumentError, "Don't argue with me!"
回答by pjs
www.ruby-doc.orgis your friend. When I googled rubydoc fail"Kernel" was the first hit. My advice is, when in doubt, go to the definitive source for definitional stuff like this.
www.ruby-doc.org是您的朋友。当我用谷歌搜索rubydoc fail“内核”时,第一个命中。我的建议是,如果有疑问,请访问此类定义性内容的权威来源。

