gpt4 book ai didi

ruby - 如何在 Ruby 中定义复合运算符(例如 +=)?

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:45 28 4
gpt4 key购买 nike

因此,我尝试为 Ruby 类定义运算符 ++=,然后我尝试:

class A
def +(x)
end

def +=(x)
end
end

Ruby 喜欢 + 但不喜欢 +=,给我这个错误:

syntax error, unexpected '=', expecting ';' or '\n'

我做错了什么?

最佳答案

你不能在 ruby​​ 中重载赋值运算符

Overloading Operators - Ruby

关于ruby - 如何在 Ruby 中定义复合运算符(例如 +=)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11107330/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com