Swaps Swaps - 3 months ago 9
Ruby Question

Ruby self assign float variable with methods like .round()

I have come across a very common scenario as follows-

var_float += 1234.1234
var_float = var_float.round(2)


Here the value for
var_float
is getting calculated with some complicated expressions and there I cannot do like
var_float += 1234.1234.round(2)
this. Hence one more line to reassign the same.

I was wondering if there's any better way to do this ruby?
For ex. if I can do something like
var_float .= round(2)
?

Answer

Sure you can:

var_float = (var_float+1234.1234).round(2)
Comments