Ruby 你能在rescue修改器中放置多个赋值吗?
假设我有这个:Ruby 你能在rescue修改器中放置多个赋值吗?,ruby,variable-assignment,modifier,Ruby,Variable Assignment,Modifier,假设我有这个: item_one = Object.find(1) rescue item_one, value = [Object.first, true] 这实际上是行不通的。它返回以下内容: syntax error, unexpected '=', expecting $end 有人知道如何在语法上把多重赋值放在rescue修饰符中吗 旁注: Boris建议通过以下方式建立救援声明: begin i1 = Object.find 1 rescue NoMethodError
item_one = Object.find(1) rescue item_one, value = [Object.first, true]
这实际上是行不通的。它返回以下内容:
syntax error, unexpected '=', expecting $end
有人知道如何在语法上把多重赋值放在rescue修饰符中吗
旁注:
Boris建议通过以下方式建立救援声明:
begin
i1 = Object.find 1
rescue NoMethodError
i1, v = Object.first, true
end
使用括号。因此,您正在从任务中解救:
(item_one = Object.find(1)) rescue item_one, value = [Object.first, true]
Touché,这使得它如此
item_one
代表item_one
和value
的数组,我非常喜欢将Ruby行变长,但是救援一行程序实际上只适用于最简单的情况。@BorisStitnicky您有什么建议吗?多行救援:begini1=Object.find 1;救援指名员;i1,v=Object.first,true end
。它消除了语法上的歧义,并为您指定要挽救的错误提供了巨大的优势。相信我,onelinerrescue
只是一种非常不健康的语法糖果,请务必谨慎使用。谢谢@BorisStitnicky的提示!