I stumbled upon this line of Ruby in our codebase:
value = value.to_i if value == value.to_i
and I can't understand what it is supposed to do.
If the guard condition is false, it does nothing. If it is true, it's a noop.
What gives?
I stumbled upon this line of Ruby in our codebase:
value = value.to_i if value == value.to_i
and I can't understand what it is supposed to do.
If the guard condition is false, it does nothing. If it is true, it's a noop.
What gives?
This line of code will cast any integer number to the Integer class, so it will replace 2.0 or BigDecimal(2) with 2. This might be useful if you have code later on which cares about the class of value.
It is a noop in most cases. However, in cases where value doesn't respond to to_i, a NoMethodError will be thrown - I very much doubt that this is intentional, but without more context it's difficult to be more precise.