Ruby-在一行中检索值、验证和分配值
这里是检索到的(Ruby-在一行中检索值、验证和分配值,ruby,Ruby,这里是检索到的(order.total\u cost.to\u f rescue nil)、验证的(如果tc)和分配的(msg.totalCost=tc)。我想知道是否所有操作都可以在一行中进行,而不必检索订单。总成本两次(假设检索是一项耗时较长的消费操作)。是的,如果必须: tc = order.total_cost.to_f rescue nil msg.totalCost = tc if tc 这是相同的逻辑,可能更容易理解: (tc = order.total_cost.to_f re
order.total\u cost.to\u f rescue nil
)、验证的(如果tc
)和分配的(msg.totalCost=tc
)。我想知道是否所有操作都可以在一行中进行,而不必检索订单。总成本
两次(假设检索是一项耗时较长的消费操作)。是的,如果必须:
tc = order.total_cost.to_f rescue nil
msg.totalCost = tc if tc
这是相同的逻辑,可能更容易理解:
(tc = order.total_cost.to_f rescue nil) and (msg.totalCost = tc)
如果你愿意使用分号,并且你知道二传手不会提高,这是更好的:
if (tc = order.total_cost.to_f rescue nil) then (msg.totalCost = tc) end
无法想象有人会如此坚决地保存行以允许这种违反可读性的罪行:)
begin; msg.totalCost = order.total_cost.to_f; rescue; end