color = :red 和 self.color = :orange,为什么第一个是本地变量赋值,而第二个是方法调用?为什么第一个不是隐含的 对 self 调用 color=() 方法呢?
class TrafficLight
attr_accessor :color
def progress_color
case color
when :orange
#Don't do this!
color = :red
when :green
#Do this instead!
self.color = :orange
else
raise NotImplementedError, "What should be done if color is already :red? Check with the domain expert, and build a unit test"
end
end
end
traffic_light = TrafficLight.new
traffic_light.color = :green
traffic_light.progress_color
traffic_light.color # Now orange
traffic_light.progress_color
traffic_light.color # Still orangea