【发布时间】:2012-03-28 23:22:41
【问题描述】:
我想用类似救援的语法来实现一些东西。
begin
raise "Failed"
rescue Exception => e
puts e
end
这可行,并且 e 被分配给相应的值。但是在方法中使用,这会引发一个异常,说 e 是未定义的。换句话说,rescue 怎么能以这种方式给 e 赋值而不抛出未定义的错误呢?
class MyClass
def to_s
"hello"
end
end
def my_method
puts e.to_s
end
my_method(MyClass => e)
#=> undefined local variable or method `e' for main:Object
【问题讨论】:
-
为什么不直接将 MyClass 作为参数传递?
-
其实我只是想知道rescue是怎么工作的... :-)
-
这是一个句法结构,是 lex/parse 过程的一部分。您必须更改语言。
标签: ruby