operations Flashcards
What is the difference between modulo and remainder operations?
Modulo operations return a positive integer when the second operand is positive, and a negative integer when the second operand is negative.
Remainder operations return a positive integer when the first operand is positive, and a negative integer when the first operand is negative.
What is the preferred operator or method to use when working with negative integers?
Not sure. Need more information…
In general, you want to avoid this problem – if you need to determine the modulo or remainder of two integers, try to work with positive integers exclusively. If you can’t, then make sure you know exactly which operator or method you need to use when working with negative integers.
*=
Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand.
c *= a is equivalent to c = c * a
The splat operator (*) is interesting because it does something you can’t do without it.
Let’s say you have an array like this:
attributes = [:title, :author, :category]
And you want to use this array with a method that takes variable arguments, like attr_reader.
Then you could do this:
attr_reader *attributes
The splat operator converts the array into a list of its elements. So it would be like taking away the array & replacing it with everything inside it.
In other words, the last example translates to:
attr_reader :title, :author, :category
=~, !~, ~
Look up
!!
When using the double not-operator, the first bang makes the data it’s operating on into a Boolean before negating it. A truthy value would become the Boolean ‘false’ and a falsey value would become the Boolean ‘true.’ The second bang then flips the resultant Boolean back to the appropriate value. Thus, a double bang would make a truthy value into the Boolean ‘true,’ and the same for falsey to ‘false.’ In Ruby, only false and nil are falsey, so the double bang isn’t all that useful.