1

ruby 是否支持具有以下代码的语法:

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

我发现这个语法是有效的,并且 ruby​​ 解释器没有为此标记任何异常。如果这是有效的,任何人都可以解释这种语法的用法。

目前使用 Ruby 2.1.1

4

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

于 2014-09-10T09:25:36.237 回答