2016-08-20 54 views
0

我在嘗試將觸發器應用於以下場景時遇到問題。說我有以下的數組:撥動開關/ T-Flip翻轉

[ 'on', 
    'random text', 
    'random text', 
    'random text', 
    'random text', 
    'off' 
    'random text', 
    'random text', 
    'on' 
    'random text', 
    'off' 
    'random text', 
    'random text', 
    etc... 

我想切換到根據兩個值中的一個(如通過「上」和陣列中的「斷開」表示)翻轉

任何幫助理解

+0

*「我無法確定觸發真/假的值,因爲文本總是隨機的。」*請擴展並解釋您的意思。 – user3386109

+1

與紅寶石觸發器,你可以執行這個動作,其中兩個條件一個接一個滿足,但它們是由一個特定的值觸發(道歉,如果我沒有解釋得很好,請看這裏的例子:http://nithinbekal.com/posts/ruby​​-flip-flop /) – DawnFreeze

+0

這解釋得很好。 – user3386109

回答

3

在這裏您的數據是一個簡單的觸發器示例。

x = [ 'on', 
    'random text', 
    'random text', 
    'random text', 
    'random text', 
    'off', 
    'random text', 
    'random text', 
    'on', 
    'random text', 
    'off', 
    'random text', 
    'random text' 
] 

x.each do |text| 
    if text['on'] .. text['off'] 
    puts text 
    end 
end 

#on 
#random text 
#random text 
#random text 
#random text 
#off 
#on 
#random text 
#off 
+0

當然:)謝謝 – DawnFreeze