【发布时间】:2016-01-21 23:08:53
【问题描述】:
我有一些通过 rspec 测试的代码。看来我不需要 end 和 Ruby 中的双 rescue,甚至不需要 begin。本质上,当遇到 Geocoder 错误时我返回 5,如果遇到 CustomError 则返回 6,否则返回 7。
def get_stuff
puts "some code work"
rescue ::Geocoder::Error => e
puts "hello"
return 5
rescue CustomError => e
puts "world"
return 6
7
end
【问题讨论】:
-
如果您阅读一些关于使用
rescue的教程可能会有所帮助。这是我看过的所有 Ruby 书籍的一部分。 -
函数定义有一个隐式的开始块。所以
rescue位于def块上,就像在其他情况下它位于显式begin块上一样。正如其他人所提到的,缩进在 Ruby 中毫无意义。