i am basically coming from java background and struggling to understand the modulo operation in Ruby.
(5 % 3)
(-5 % 3)
(5 % -3)
(-5 % -3)
The sign of the remainder changes from language to language. The "right" way to do it is up for debate...
Ruby implements both ways. The remainder
function has the same behavior as Java, while modulo
and the %
operator use the other algorithm.
Wikipedia has a list of programming languages and how they implement modulo.