Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby-在一行中检索值、验证和分配值_Ruby - Fatal编程技术网

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