2015-06-28 60 views
0

我正在讀一段代碼是這樣的:`end`用或運營商

def majority_agreed_commit 
    current_commit = log_container.last_commit 

    current_term_match_indices = match_indices.values.map do |match_index| 
    match_index.downto(current_commit).find do |i| 
     log_container.term(i) == current_term 
    end || current_commit 
    end 

我不明白這是什麼意思:end || current_commit。有人能幫助我嗎?

回答

4

match_index.downto(current_commit).find do |i| 
    log_container.term(i) == current_term 
end 

返回一個值,該值與current_commit邏輯相加,結果被產生在最上面的map

match_indices.values.map do |...| 
    BLOCK_RESULT || current_commit 
end 
+0

你的意思是,每次的結果將是新的「current_commit」將在下一次迭代中使用?這是什麼「||」操作符在這裏呢? – BufBills

+0

ruby​​中的布爾OR或者被評估爲第一個操作數,除非它是'falsey'(或者是'nil'或者'false'),否則它會得到第二個操作數的值。 http://ruby.about.com/od/control/a/Boolean-Operators.htm – mudasobwa

+0

謝謝。雖然,從來沒有在cpp,java,python或js中看到過這種有趣的語法。 – BufBills