我使用超过120长度的rescue
语句,因为Rubocop
显示了违法行为。什么应该是最好的写作方式?
原始声明:
rescue ActiveResource::ResourceNotFound, ActiveResource::BadRequest, ActiveResource::TimeoutError, ArgumentError => e
修改后的声明:
rescue ActiveResource::ResourceNotFound, ActiveResource::BadRequest, ActiveResource::TimeoutError,
ArgumentError => e
但它看起来并不正确,也不可读。写它的最佳方式是什么?
答案 0 :(得分:2)
当我有一长串的拯救错误列表时,我通常会这样处理:
class MyClass < Object
HandleTheseErrors = [
ActiveResource::ResourceNotFound,
ActiveResource::BadRequest,
ActiveResource::TimeoutError,
ArgumentError
]
def do_something
begin
# do something error prone
rescue *HandleTheseErrors => e
puts e
end
end
end
*HandleTheseErrors
表示,将HandleTheseErrors
数组的每个元素作为参数传递给救援方法。