1
tc = order.total_cost.to_f rescue nil
msg.totalCost = tc if tc

here is total_cost retrieved (order.total_cost.to_f rescue nil), validated (if tc) and assigned (msg.totalCost = tc). I wonder whether all the operations could be made in one line without having to retrieve order.total_cost twice (assume that retrieval is a heavy time consumer operation).

Paul
  • 25,812
  • 38
  • 124
  • 247

1 Answers1

1

Yes, you can do it on one line if you must:

(tc = order.total_cost.to_f rescue nil) and (msg.totalCost = tc)

This is the same logic and may be a bit easier to comprehend:

if (tc = order.total_cost.to_f rescue nil) then (msg.totalCost = tc) end

If you're willing to use semi-colons, and you know the setter won't raise, this is better:

begin; msg.totalCost = order.total_cost.to_f; rescue; end
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119