2016-02-23 40 views
3

我試圖在軌道4軌道4 - 方法來檢查是否所有的屬性都爲真

我想在一個模型編寫一個方法來檢查是否所有的屬性都是真實的應用程序。

我想:

def ready_to_go 
    if 
    [ payment == true, 
    && terms == true 
    && identification == true 
    && key_org == true 
    && key_business == true 
    && docs == true 
    && funding == true 
    && contract == true 
    && governance == true 
    && internal == true 
    && preferences == true 
    && address == true 
    && interest == true ] 
    end 
    end 

任何人都可以看看有什麼不對的?

+0

定義錯誤,你的意思是說它可以更簡潔嗎?或者它不工作?我不明白爲什麼你有數組語法'['和']'或者爲什麼它在'if'中沒有任何內容。如果你只想返回true或false,那麼除去if和數組語法 –

+0

,而不是在模型'self.attributes'中嘗試像這樣獲得該模型的所有屬性。 –

回答

3

[...]是錯誤的。它定義了一個數組。對一個元素「假」的歪曲被解釋爲是真的。只要刪除括號(或使用圓括號,如果你真的需要他們不會感到困惑)。

並在同一行開始您的if。 如果您只想返回true/false,則可以完全刪除if。 如果你的價值觀是true或任falsenil,你也不需要對證真:

def ready_to_go 
    payment && 
    terms && 
    identification && 
    ... 
end 
3

最簡單的方法來定義方法,並檢查它是否準備好去還是不去,那麼:

def ready_to_go 
    [ payment, terms, identification, key_org, key_business, docs, funding, contract, governance, internal, preferences, address, interest].all? 
end 
2

嘗試Array.all?

[true, false].all? # false 
[true, true].all? # true 
1

首先,使用?符號定義方法作爲謂詞。然後刪除與真實的比較payment && terms && ...

def ready_to_go? 
    payment && 
    terms && 
    ... 
end 
相關問題