正如我一直在邏輯中所學的那樣,“and”運算子意味著兩個值都必須為真,整個陳述句都為真。如果您有許多用“and”鏈接的陳述句,那么其中任何一個為假的都應該使整個宣告為假。然而,在 Ruby 中,我遇到了這種情況:
horizon_flat = true
one_up_and_down = true
magellan_fell = false
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
puts("Hey ruby, doesn't the horizon look flat?")
puts(horizon_flat) # true
puts("Isn't there only one up and one down?")
puts(one_up_and_down) # true
puts("Did Magellan fall off the earth?")
puts(magellan_fell) # false
puts("Is the earth flat?")
puts(flat_earth_thesis) # true
奇怪的是,如果我只是運行陳述句本身,它會正確回傳 false puts(horizon_flat and one_up_and_down and magellan_fell) # false
但是如果我將該陳述句存盤在一個變數中,然后呼叫它,該變數會輸出 true。為什么紅寶石認為地球是平的?
uj5u.com熱心網友回復:
雖然您期望這樣:
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
被評估為:
flat_earth_thesis = (horizon_flat and one_up_and_down and magellan_fell)
相反,它被評估為:
(flat_earth_thesis = horizon_flat) and one_up_and_down and magellan_fell
如評論中所述,查看運算子優先級。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411410.html
標籤:
上一篇:如何修復無法延遲初始化集合
