operations Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the difference between modulo and remainder operations?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the preferred operator or method to use when working with negative integers?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

*=

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The splat operator (*) is interesting because it does something you can’t do without it.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

=~, !~, ~

A

Look up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

!!

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly