2016-02-23 34 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 
相关问题