xuinkrbin.
xuinkrbin.

Reputation: 1015

Can One disambiguate between simple operators and compound assignment operators in ruby?

Can Anyone point Me in the direction of how to tell when, for example, the + operator is used in ruby as opposed to the += operator from the inside of the + operator's definition? To illustrate:

class A
    def +(b)
        if is_theCallActuallyACompoundAssignment?
            compoundAssignment = true
        else
            compoundAssignment = false
        end
        doOtherStuff
    end
end

Is there a Kernel method, perhaps?

Upvotes: 1

Views: 84

Answers (1)

robbrit
robbrit

Reputation: 17960

This code:

a += 5

Gets translated to this:

a = a + 5

Your + method will not know that you received a compound assignment.

Upvotes: 5

Related Questions