开发者

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

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.


% 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.


In answer to the question "What does the % symbol do or mean in Ruby?" It is:

1) The modulo binary operator (as has been mentioned)

17 % 10 #=> 7 

2) The alternative string delimiter token

%Q{hello world} #=> "hello world"
%Q(hello world) #=> "hello world"
%Q[hello world] #=> "hello world"
%Q!hello world! #=> "hello world"
# i.e. choose your own bracket pair
%q(hello world) #=> 'hello world'
%x(pwd)         #=> `pwd`
%r(.*)          #=> /.*/

3) The string format operator (shorthand for Kernel::sprintf)

"05d" % 123 #=> "00123"


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

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


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.


Also keep in mind that, Ruby's definition of the modulo (%) operator differs from that of C and Java. In Ruby, -7%3 is 2. In C and Java, the result is -1 instead. In Ruby, the sign of the result (for % operator) is always the same as the sign of the second operand.


Its the modulo operator.

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


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.


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]


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜