在ruby中,可以消除简单运算符和复合赋值运算符之间的歧义吗?

在ruby中,可以消除简单运算符和复合赋值运算符之间的歧义吗?,ruby,operators,Ruby,Operators,有人能告诉我如何判断何时在ruby中使用+运算符,而不是从+运算符的定义内部使用+=运算符吗?举例说明: class A def +(b) if is_theCallActuallyACompoundAssignment? compoundAssignment = true else compoundAssignment = false end doOtherStuff

有人能告诉我如何判断何时在ruby中使用+运算符,而不是从+运算符的定义内部使用+=运算符吗?举例说明:

class A
    def +(b)
        if is_theCallActuallyACompoundAssignment?
            compoundAssignment = true
        else
            compoundAssignment = false
        end
        doOtherStuff
    end
end
可能有内核方法吗?

以下代码:

a += 5
转换为:

a = a + 5
您的+方法将不知道您收到了复合分配。

此代码:

a += 5
转换为:

a = a + 5

你的+方法不会知道你收到了一个复合赋值。

不赞成使用+=而不是+。恐怕目前还不可能。好吧,希望Ruby 2.0中会出现这种功能-我不赞成使用+=而不是+。恐怕目前还不可能。好吧,希望Ruby 2.0中能出现这种功能-