ruby 是否支持具有以下代码的语法:
class Test
def test
#some code here
else
#some code here
end
end
我发现这个语法是有效的并且 ruby 解释器没有为此标记任何异常。 如果这是有效的,任何人都可以解释此语法的用法。
目前使用 Ruby 2.1.1
最佳答案
这是有效的 Ruby 语法,但对于 rescue
看例子:
#exm.rb
class Test
def test
#some code here
else
#some code here
end
end
并运行(-w 为您的脚本打开警告
):
$ ruby -w exm.rb
exm.rb:7: warning: else without rescue is useless
检查语法(-c 仅检查语法
):
ruby -c exm.rb
arra.rb:7: warning: else without rescue is useless
Syntax OK
用救援
:
#exm.rb
class Test
def test
#some code here
rescue
#some code here
else
#some code here
end
end
检查语法:
ruby -c exm.rb
Syntax OK
关于ruby - Ruby 2.1.1 中使用 else 语句的定义语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25761710/