- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
"中的 "rescue Exception => e"有什么作用?-6ren"> "中的 "rescue Exception => e"有什么作用?-举个例子: def method_of_doom my_string = "I sense impending doom." my_string.ah_ha_i_called_a_nonexisten-6ren">
举个例子:
def method_of_doom
my_string = "I sense impending doom."
my_string.ah_ha_i_called_a_nonexistent_method
rescue NoMethodError => e:
puts "PROBLEM: " + e.to_s
rescue Exception:
puts "Uhh...there's a problem with that there method."
end
在它说的那一行:
rescue NoMethodError => e:
'=>' 在做什么?
它与这种用法有何不同:
module FighterValues
BAMBOO_HEAD = { 'life' => 120, 'hit' => 9 }
DEATH = { 'life' => 90, 'hit' => 13 }
KOALA = { 'life' => 100, 'hit' => 10 }
CHUCK_NORRIS = { 'life' => 60000, 'hit' => 99999999 }
def chuck_fact
puts "Chuck Norris' tears can cure cancer..."
puts "Too bad he never cries."
end
end
module ConstantValues
DEATH = -5 # Pandas can live PAST DEATH.
EASY_HANDICAP = 10
MEDIUM_HANDICAP = 25
HARD_HANDICAP = 50
end
puts FighterValues::DEATH
→ {'life'=>90,'hit'=>13}
puts ConstantValues::DEATH
→ -5
最佳答案
哈希火箭实际上是一个句法标记。您可以在 ext/ripper/ripper.y
定义的语法中找到 token :
%token tASSOC /* => */
换句话说,Ripper 使用哈希火箭关联事物。
通常,此标记用于散列文字中以将键与值相关联。例如:
{ :e => 'foo' }
将字符串文字 foo
与符号 :e
相关联。这种常见用法就是为什么人们倾向于将哈希火箭仅视为与哈希相关的结构。
另一方面,以下将变量与异常相关联:
rescue => e
在这种情况下,Ripper 不是将键与值相关联,而是将变量 e
与隐含的 StandardError
异常相关联,并使用该变量存储值的 Exception#message .
如果您了解分词器、词法分析器和解析器,ripper.y
和 ext/ripper/lib/ripper
的各种内容都具有指导意义。然而,在 Ruby Under a Microscope 的第 19 页,Pat Shaughnessy 警告说:
Ruby doesn’t use the Lex tokenization tool, which C programmers commonly use in conjunction with a parser generator like Yacc or Bison. Instead, the Ruby core wrote the Ruby tokenization code by hand.
当您尝试在源代码级别理解 Ruby 的语法时,请牢记这一点。
关于ruby - "=>"中的 "rescue Exception => e"有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14390182/
我无法确定以下 ruby 内联救援代码有什么问题 def test_check() p "first st" t = 5 * lsdj rescue return false p "second
我知道全局变量 $! 包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法? rescue $! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这
class A def a_method #.. end end class B e p e.message end # ... end 我想在 B 类中的每个方法(
我会定义值(value)。但是这个值可能在散列的键值中。如果此键不存在,我将使用 rescue 定义值为 nil。例如 foo = bar[:a][:b][:c] 救援 nil 但在实践中告诉我糟糕的
这是一个简单的例子: puts File.join(nil, "hello") 会输出 test.rb:4:in 'join': can't convert nil into String (Type
一个运行着的程序常会遇到意外的问题.一个要读取的文件不存在;当希望存入一些数据时磁盘满了;用户可能输入不恰当的数据. ruby>file=open("some_file"
我正在使用经常超时的 API 运行脚本。当发生这种情况时,我正在使用 begin/rescue block 让它进入 redo,但我想在运行 redo 之前记录命令行发生的事情 命令。 begin #
我有时会遇到一些意外错误,例如超时错误、503 错误等。有些错误我什至不知道我可能会收到。我无法通过执行以下操作来解释所有这些问题: rescue Timeout::Error => e 拯救Exce
在 Ruby 中,您可以使用 rescue => e 捕获异常,而无需显式传递任何类型的异常,例如 rescue CustomException => e。 我可以看出它并没有捕获所有异常,因为: d
我想将以下 erb 代码转换成 slim。 我的做法是: - begin = some_function - rescue Exception = some_other_fu
我理解救援的概念——我的问题是: 当我实际创建一个对象时,我应该在我的类中编写我的begin rescue end 代码还是应该在类之外编写它?如果我有两个文件 - 一个是我的类(class),一个是
局部变量 begin transaction #Code inside transaction object = Class.new attributes raise unles
在 ruby 中,要捕获错误,可以使用 rescue 语句。通常,此语句发生在 begin 和 end 之间。还可以将 rescue 语句用作 block (do ... end) 或方法 (de
我正在努力将以下代码作为后台作业发送,同时不时遇到一个或另一个错误, 我的应用程序/ worker /brassring.rb 是 require File.dirname(__FILE__)+"/b
我已将 Resque 添加到我的 Rails 3 项目中。我创建了一个从/向数据库读取/写入一些东西的作业。 问题是我在终端中看不到 SQL 查询日志,例如 "Post Load (0.5ms) SE
我正在尝试通过 Resque 发送 Devise 电子邮件。 定期电子邮件通过 Resque 发送就好了。并且设计电子邮件发送得很好,但不是通过 Resque 发送设计电子邮件。我得到“找不到有效的映
我有一组连接到各种 API 的 Adapter 类,等等。以下是每个适配器如何设置的简单通用示例: class AmazonAdapter include Sidekiq::Worker
This question already has answers here: How does one use rescue in Ruby without the begin and end bl
在 Ruby koans 的第 6 个练习中,有: def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil #
我的 Grape 应用程序有几个错误处理程序,最后包括: rescue_from :all, backtrace: true do |e| message = { errors: { all: e
我是一名优秀的程序员,十分优秀!