Reputation: 71
Sorry in advance if I am not asking my question in the correct way.
Trying to get this bit of Ruby code to work. What I don't understand is how to make the click do at the bottom call the yield functions (it's a traffic light) so that a click will cycle through the yield options. The true, false, false will mean the light is red because it's on at the top and the 2 bottom ones are false. I'm also having a hard time wrapping my head around enumerator and yield.
class TrafficLight
include Enumerable
include TL
def each
yield [true, false, false]
yield [true, true, false]
yield [false, false, true]
yield [false, true, false]
end
end
class Bulb < Shoes::Shape
attr_accessor :stack
attr_accessor :left
attr_accessor :top
attr_accessor :switched_on
def initialize(stack, left, top, switched_on = false)
self.stack = stack #don't change.
self.left = left
self.top = top
self.switched_on = switched_on
draw left, top, bulb_colour
end
# HINT: Shouldn't need to change this method
def draw(left, top, colour
)
stack.app do
fill colour
stack.append do
oval left, top, 50
end
end
end
def bulb_colour
"#999999"
end
end
class GoBulb < Bulb
def bulb_colour
"#00FF30"
end
end
class WaitBulb < Bulb
def bulb_colour
"#FFFC00"
end
end
class StopBulb < Bulb
def bulb_colour
"#FF0000"
end
end
module TL
Go = "#00FF30"
Wait = "#FFFC00"
Stop = "#FF0000"
end
Shoes.app :title => "My Amazing Traffic Light", :width => 150, :height => 250 do
background "000", :curve => 10, :margin => 25
stroke black
@traffic_light = TrafficLight.new
@top = StopBulb.new self, 50, 40, true
@middle = Bulb.new self, 50, 100, true
@bottom = Bulb.new self, 50, 160, true
click do #make this switch on/off depending on what you click. Only 1 should be on
end
end
I've Googled and searched but the enumerator examples I got didn't allow me to do what I needed to do. Any insight is greatly appreciated.
Upvotes: 1
Views: 194
Reputation: 14082
The Ruby (1.9.1) packed with current version of Shoes has some unexpected behavior on Enumerator#next
. It gets stuck when this method is called. So you cannot iteratively retrieve values from an Enumerator
.
The TrafficLight
class has to be rewrited to mimic the Enumerator#next
. If there isn't such bug on next
, we can use TraficLight.new.cycle
and repeatedly call `next on this cycling enumerator.
module TL
Go = "#00FF30"
Wait = "#FFFC00"
Stop = "#FF0000"
end
class TrafficLight
include TL
STATUS = [
[true, false, false],
[true, true, false],
[false, false, true],
[false, true, false]
]
def initialize; @index = 0; end
def current
STATUS[@index % STATUS.size]
end
def next
@index += 1
current
end
end
Update Bulb
, add a method update(on)
to redraw the bulb.
def update(on = false)
self.switched_on = on
draw self.left, self.top, on ? self.bulb_colour : '#999999'
end
And update the main logic of Shoes.app
to use specific bulb:
@traffic_light = TrafficLight.new
@top = StopBulb.new self, 50, 40, true
@middle = WaitBulb.new self, 50, 100, false
@bottom = GoBulb.new self, 50, 160, false
click do #make this switch on/off depending on what you click. Only 1 should be on
status = @traffic_light.next
[@top, @middle, @bottom].zip(status).each do |light, on|
light.update(on)
end
end
Upvotes: 2