The += operator += not associated with any of the methods, it is just syntactic sugar, when you write a += b the Ruby interpreter will convert it to a = a + b , the same for ab += c , which will be converted to ab = ab + c . So you just need to define the methods x= and x as needed:
class Plane def initialize @moved = 0 @x = 0 end attr_reader :x def x=(x) @x = x @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
source share