My rails app just started receiving this error today. Here is the code context. It is throwing the error on the line that starts with new_host_id
while @host_ids.include?(new_host_id)
i++
new_host_id = duplicate_host_id + i.to_s
end
Ruby does not have a ++ operator.
The idiom in Ruby is i += 1, which is the abbreviated form of i = i + 1.
Initially I thought that the posted code was incorrect and had to be ++i to generate that error. However, as Jörg W Mittag explains in a comment, this is not the case:
[..] Ruby allows whitespace (including line breaks) between an operator and the operand(s), so the entire thing is interpreted as
i + (+(new_host_id = duplicate_host_id + i.to_s))[.. which is] why the NoMethodError refers to String.
Here is a simplified example showing the issue (the posted code refers to the first case):
> x = "hello" > +x undefined method `+@' for "hello":String (NoMethodError) > x+ syntax error, unexpected $end
I used + and not ++ above to simplify the example: Ruby treats ++i and i++ as the productions +(+i) and [roughly] i+(+) ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With