2014-09-10 134 views
1

else语句定义语法将支持Ruby语法与下面的代码:与红宝石2.1.1

class Test 
def test 
    #some code here 
else 
    #some code here 
end 
end 

我发现这个语法是有效和Ruby解释器没有标志为这个任何异常。 如果这是有效的,任何人都可以解释这种语法的用法。

目前使用Ruby 2.1.1

回答

5

,这是合法的Ruby语法但rescue外观例如:

#exm.rb 
class Test 
def test 
    #some code here 
else 
    #some code here 
end 
end 

和运行(-w turn warnings on for your script):

$ ruby -w exm.rb 
exm.rb:7: warning: else without rescue is useless 

检查语法(-c check syntax only) :

ruby -c exm.rb 
arra.rb:7: warning: else without rescue is useless 
Syntax OK 

rescue

#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 

阅读begin + rescue + else

+0

我不知道多久我写了这对SO了,但显然它不能重复足够:**阅读警告**! – 2014-09-10 13:33:27