Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以,我正在尝试定义运算符+和+=一个 Ruby 类,我尝试:
+
+=
class A def +(x) end def +=(x) end end
Ruby 喜欢+但不喜欢+=,给我这个错误:
语法错误,意外 '=',期待 ';' 或'\n'
我做错什么了?
您a += b被解释为a = a + b并且仅+使用运算符。您也不必定义+=运算符
a += b
a = a + b
您不能在 ruby 中重载赋值运算符
重载运算符 - Ruby