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

后端 未结 9 1922
甜味超标
甜味超标 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:21

    Regardless of how it works, the modulo operator is probably not the best code for the purpose (even though we are not given much context). As Jörg mentioned in a comment, the expression if counter.odd? is probably the intent, and is more readable.

    If this is view code and used to determine (for example) alternating row colors, then you may be able to do without the counter altogether by using the built-in Rails helper cycle(). For example, you can use cycle('odd','even') as a class name for a table row, eliminating the counter and surrounding if/then logic.

    Another thought: if this is within an each block, you may be able to use each_with_index and eliminate the extraneous counter variable.

    My refactoring $0.02.

    0 讨论(0)
  • 2021-02-02 10:24

    It is the modulo operator, which is a fancy way of saying it's the remainder operator.

    So if you divided a number by two, and the integer remainder of that number is one, then you know the number was odd. Your example checks for odd numbers.

    Often this is done to highlight odd number rows with a different background color, making it easier to read large lists of data.

    0 讨论(0)
  • 2021-02-02 10:28

    To give a few ways to say it:

    • Modulo operator
    • Remainder operator
    • Modular residue

    Strictly speaking, if a % b = c, c is the unique constant such that

    a == c (mod b) and 0 <= c < b

    Where x == y (mod m) iff x - y = km for some constant k.

    This is equivalent to the remainder. By some well known theorem, we have that a = bk + c for some constant k, where c is the remainder, which gives us a - c = bk, which obviously implies a == c (mod b).

    (Is there a way to use LaTeX on Stackoverflow?)

    0 讨论(0)
  • 2021-02-02 10:31

    % is the modulo operator. The result of counter % 2 is the remainder of counter / 2.

    n % 2 is often a good way of determining if a number n is even or odd. If n % 2 == 0, the number is even (because no remainder means that the number is evenly divisible by 2); if n % 2 == 1, the number is odd.

    0 讨论(0)
  • 2021-02-02 10:31

    Its the modulo operator.

    http://en.wikipedia.org/wiki/Modulo_operation

    0 讨论(0)
  • 2021-02-02 10:34

    This is a very basic question. % is the modulo opereator if counter % 2 == 1 results to true for every odd number and to false for every even number.

    If you're learning ruby you should learn how to use irb, there you can try things out and perhaps answer the question yourself.

    try to enter

    100.times{|i| puts "#{i} % 2 == 1 #=> #{i % 2 == 1}"}
    

    into your irb irb console and see the output, than it should be clear what % does.

    And you really should take a look at the rails api documentation (1.9, 1.8.7, 1.8.7), there you would have found the answer two your question % (Fixnum) with a further link to a detailed description of divmod (Numeric):

    Returns an array containing the quotient and modulus obtained by dividing num by aNumeric. > If q, r = x.divmod(y), then

    q = floor(float(x)/float(y))
    x = q*y + r
    

    The quotient is rounded toward -infinity, as shown in the following table:

     a    |  b  |  a.divmod(b)  |   a/b   | a.modulo(b) | a.remainder(b)
    ------+-----+---------------+---------+-------------+---------------
     13   |  4  |   3,    1     |   3     |    1        |     1
    ------+-----+---------------+---------+-------------+---------------
     13   | -4  |  -4,   -3     |  -3     |   -3        |     1
    ------+-----+---------------+---------+-------------+---------------
    -13   |  4  |  -4,    3     |  -4     |    3        |    -1
    ------+-----+---------------+---------+-------------+---------------
    -13   | -4  |   3,   -1     |   3     |   -1        |    -1
    ------+-----+---------------+---------+-------------+---------------
     11.5 |  4  |   2,    3.5   |   2.875 |    3.5      |     3.5
    ------+-----+---------------+---------+-------------+---------------
     11.5 | -4  |  -3,   -0.5   |  -2.875 |   -0.5      |     3.5
    ------+-----+---------------+---------+-------------+---------------
    -11.5 |  4  |  -3,    0.5   |  -2.875 |    0.5      |    -3.5
    ------+-----+---------------+---------+-------------+---------------
    -11.5 | -4  |   2    -3.5   |   2.875 |   -3.5      |    -3.5
    

    Examples

     11.divmod(3)         #=> [3, 2]
     11.divmod(-3)        #=> [-4, -1]
     11.divmod(3.5)       #=> [3, 0.5]
     (-11).divmod(3.5)    #=> [-4, 3.0]
     (11.5).divmod(3.5)   #=> [3, 1.0]
    
    0 讨论(0)
提交回复
热议问题