I was running some code in here. I tried -40 % 3. It gives me the output 2. when I performed the same operation in C, I get:
-40 % 3
2
int i
In Java and C, the result of the modulo operation has the same sign as the dividend, hence -1 is the result in your example.
In Ruby, it has the same sign as the divisor, so +2 will be the result according to your example.