相关疑难解决方法(0)

可以覆盖/实现的ruby运算符列表

是否有可以覆盖的所有ruby运算符的列表?(不是那些不能!)

ruby operator-overloading operators

24
推荐指数
1
解决办法
6833
查看次数

用于+ =的Ruby方法

有没有办法让Ruby能够做到这样的事情?

class Plane
  @moved = 0
  @x = 0
  def x+=(v) # this is error
    @x += v
    @moved += 1
  end
  def to_s
    "moved #{@moved} times, current x is #{@x}"
  end
end

plane = Plane.new
plane.x += 5
plane.x += 10
puts plane.to_s # moved 2 times, current x is 15
Run Code Online (Sandbox Code Playgroud)

ruby operator-overloading

5
推荐指数
2
解决办法
519
查看次数

标签 统计

operator-overloading ×2

ruby ×2

operators ×1