2013-03-28 56 views
1

對不起,如果我不以正確的方式問我的問題。Ruby Enumerable和Yield的使用情況

試圖讓這一點的Ruby代碼工作。我不明白的是如何讓底部的點擊操作調用yield函數(這是一個交通燈),以便點擊將循環訪問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 

我GOOGLE了搜查,但枚舉的例子我沒有讓我做什麼,我需要做的。任何見解都非常感謝。

+0

你需要循環中的枚舉(即,它應該回到紅黃色之後)? – 2013-03-28 11:29:29

+0

嗨,歡迎來到堆棧溢出。我認爲你的問題可以說得更好。試着弄清楚你的問題是什麼。此外,你有複製/粘貼了很多代碼。如果將其減少到僅僅是爲了使問題真正需要的線條,人們將更有可能花時間閱讀它。 – 2013-03-28 11:49:29

回答

2

裝有當前版本Shoes的Ruby(1.9.1)在Enumerator#next上有一些意外的行爲。當這個方法被調用時它會卡住。所以你不能迭代檢索Enumerator的值。

TrafficLight類必須重新編寫以模仿Enumerator#next。如果在next上沒有這樣的錯誤,我們可以使用TraficLight.new.cycle並在該循環枚舉器上反覆調用next。

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 

更新Bulb,添加一個方法update(on)重繪燈泡。

def update(on = false) 
    self.switched_on = on 
    draw self.left, self.top, on ? self.bulb_colour : '#999999' 
end 

和更新Shoes.app主要邏輯使用特定的燈泡:

@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