Hegwin.Me

疏影横斜水清浅,暗香浮动月黄昏。

Ruby 2.5 允许在do/end中使用rescue

In Ruby 2.4

(-1).upto(1).each do |i|
  p 8 / i
rescue e
  p e
end

You will get SyntaxError: unexpected keyword_rescue, expecting keyword_end.

In Ruby 2.5

(-1).upto(1).each do |i|
  p 8 / i
rescue e
  p e
end

You will get:

-8
#<ZeroDivisionError: divided by 0>
8

Tags:

Posted on: 2018-01-31

Back