Skip to main content
class Machine
  
  def start
      flip_switch(:on)
  end

  def stop
       flip_switch(:off)
  end
  
  private
  
  attr_writer :switch

  def flip_switch(desired_state)
       self.switch = desired_state
  end
end

You need self prepended to a private setter method. Unlike other private method where self is prohibited. This is to distinguish between setting a local variable and the instance method setter.

Comments