在Ruby中,fail关键字做什么?

时间:2023-01-04 00:43:34

I am learning Ruby and encountered the fail keyword. What does it mean?

我正在学习Ruby并遇到了fail关键字。这是什么意思?

if password.length < 8
   fail "Password too short"
end
unless  username
   fail "No user name set"
end

4 个解决方案

#1


119  

In Ruby, fail is synonymous with raise. The fail keyword is a method of the Kernel module which is included by the class Object. The fail method raises a runtime error just like the raise keyword.

在Ruby中,失败是raise的同义词。fail关键字是由类对象包含的内核模块的一种方法。失败方法会引发运行时错误,就像raise关键字一样。

The fail method has three overloads:

故障方法有三个重载:

  • fail: raises a RuntimeError without an error message.

    失败:在没有错误消息的情况下引发运行时错误。

  • fail(string): raises a RuntimeError with the string argument as an error message:

    失败(字符串):将字符串参数作为错误消息引发运行时错误:

    fail "Failed to open file"
    
  • fail(exception [, string [, array]]): raises an exception of class exception (first argument) with an optional error message (second argument) and callback information (third argument).

    fail(exception [, string [, array]):引发类exception(第一个参数)的异常,并带有可选的错误消息(第二个参数)和回调信息(第三个参数)。

    Example: Assume you define a function which should fail if given a bad argument. It is better to raise an ArgumentError and not a RuntimeError:

    示例:假设您定义了一个函数,如果给出一个错误的参数,那么这个函数将会失败。最好提出一个ArgumentError,而不是运行时错误:

    fail ArgumentError, "Illegal String"
    

    Another Example: You can pass the whole backtrace to the fail method so you can access the trace inside the rescue block:

    另一个示例:您可以将整个回溯传递给fail方法,以便访问营救块内部的跟踪:

    fail ArgumentError, "Illegal String", caller
    

    caller is a Kernel method which returns the backtrace as an array of strings in the form file:line: in 'method'.

    调用者是一个内核方法,它以字符串数组的形式在“方法”中:line:返回。

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.

没有参数,就会抛出异常$!或者,如果$!是零。使用单个字符串参数,将字符串作为消息引发运行时错误。否则,第一个参数应该是异常类的名称(或发送异常消息时返回异常对象的对象)。可选的第二个参数设置与异常相关的消息,第三个参数是一个回调信息数组。在begin的营救条款中有例外。块结束。

Source: Ruby Documentation on the Kernel Module.

源代码:内核模块上的Ruby文档。

#2


27  

fail == raise

失败= =提高

In other words, fail is just a popular alias for raise error-raising method. Usage:

换句话说,fail只是raise error- raise方法的一个常用别名。用法:

fail ArgumentError, "Don't argue with me!"

#3


23  

Rubocop says about usage of both words;

Rubocop讨论了这两个词的用法;

'Use fail instead of raise to signal exceptions.'

“使用失败,而不是引发异常。”

'Use raise instead of fail to rethrow exceptions.'

使用加薪而不是失败地重新抛出异常。

Here is an example.

这是一个例子。

def sample
  fail 'something wrong' unless success?
rescue => e
  logger.error e
  raise
end

#4


7  

www.ruby-doc.org is 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失败时,“内核”是第一个热门。我的建议是,当有疑问的时候,去找到像这样的定义性的东西的最终来源。

#1


119  

In Ruby, fail is synonymous with raise. The fail keyword is a method of the Kernel module which is included by the class Object. The fail method raises a runtime error just like the raise keyword.

在Ruby中,失败是raise的同义词。fail关键字是由类对象包含的内核模块的一种方法。失败方法会引发运行时错误,就像raise关键字一样。

The fail method has three overloads:

故障方法有三个重载:

  • fail: raises a RuntimeError without an error message.

    失败:在没有错误消息的情况下引发运行时错误。

  • fail(string): raises a RuntimeError with the string argument as an error message:

    失败(字符串):将字符串参数作为错误消息引发运行时错误:

    fail "Failed to open file"
    
  • fail(exception [, string [, array]]): raises an exception of class exception (first argument) with an optional error message (second argument) and callback information (third argument).

    fail(exception [, string [, array]):引发类exception(第一个参数)的异常,并带有可选的错误消息(第二个参数)和回调信息(第三个参数)。

    Example: Assume you define a function which should fail if given a bad argument. It is better to raise an ArgumentError and not a RuntimeError:

    示例:假设您定义了一个函数,如果给出一个错误的参数,那么这个函数将会失败。最好提出一个ArgumentError,而不是运行时错误:

    fail ArgumentError, "Illegal String"
    

    Another Example: You can pass the whole backtrace to the fail method so you can access the trace inside the rescue block:

    另一个示例:您可以将整个回溯传递给fail方法,以便访问营救块内部的跟踪:

    fail ArgumentError, "Illegal String", caller
    

    caller is a Kernel method which returns the backtrace as an array of strings in the form file:line: in 'method'.

    调用者是一个内核方法,它以字符串数组的形式在“方法”中:line:返回。

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.

没有参数,就会抛出异常$!或者,如果$!是零。使用单个字符串参数,将字符串作为消息引发运行时错误。否则,第一个参数应该是异常类的名称(或发送异常消息时返回异常对象的对象)。可选的第二个参数设置与异常相关的消息,第三个参数是一个回调信息数组。在begin的营救条款中有例外。块结束。

Source: Ruby Documentation on the Kernel Module.

源代码:内核模块上的Ruby文档。

#2


27  

fail == raise

失败= =提高

In other words, fail is just a popular alias for raise error-raising method. Usage:

换句话说,fail只是raise error- raise方法的一个常用别名。用法:

fail ArgumentError, "Don't argue with me!"

#3


23  

Rubocop says about usage of both words;

Rubocop讨论了这两个词的用法;

'Use fail instead of raise to signal exceptions.'

“使用失败,而不是引发异常。”

'Use raise instead of fail to rethrow exceptions.'

使用加薪而不是失败地重新抛出异常。

Here is an example.

这是一个例子。

def sample
  fail 'something wrong' unless success?
rescue => e
  logger.error e
  raise
end

#4


7  

www.ruby-doc.org is 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失败时,“内核”是第一个热门。我的建议是,当有疑问的时候,去找到像这样的定义性的东西的最终来源。