What does the % operator do in Ruby in N % 2?

后端 未结 9 2082
甜味超标
甜味超标 2021-02-02 09:57

if counter % 2 == 1 I am trying to decode this line - it\'s a Rails project and I am trying to figure out what the % does in this if statement.

9条回答
  •  庸人自扰
    2021-02-02 10:45

    That's the modulo operator. It gives the remainder when counter is divided by 2.

    For example:
    3 % 2 == 1  
    2 % 2 == 0
    

提交回复
热议问题