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