這只是罰款:爲什麼在Ruby中返回a或b是一個void值表達式錯誤?
def foo
a or b
end
這也是罰款:
def foo
return a || b
end
這將返回void value expression
:
def foo
return a or b
end
爲什麼?它甚至沒有被執行;它沒有通過語法檢查。 void value expression
是什麼意思?
這只是罰款:爲什麼在Ruby中返回a或b是一個void值表達式錯誤?
def foo
a or b
end
這也是罰款:
def foo
return a || b
end
這將返回void value expression
:
def foo
return a or b
end
爲什麼?它甚至沒有被執行;它沒有通過語法檢查。 void value expression
是什麼意思?
return a or b
被解釋爲(return a) or b
,等的return a
值是需要計算的(return a) or b
價值,但由於return
從未離開了相應的值(因爲它從該位置逃逸),它的目的不是要返回原始位置的有效值。因此整個表達式留下了(some_void_value) or b
,並且卡住了。這就是它的意思。
很簡單,因爲or
具有lower precedence比||
這意味着return a
將被執行之前or b
,or b
因此不可達
這不是一個重複。我已經刪除了所有對「||」運算符的引用,問題依然存在。請重新打開。 – Shelvacu
請不要編輯您的問題以使答案無效。 http://meta.stackoverflow.com/questions/290297/how-much-change-to-the-question-is-too-much \ –
@MsYvette對不起,我應該考慮到這一點。但是,我希望它仍然可以表明,這個標記爲重複的問題不能回答我的問題。 – Shelvacu