Ruby语法错误

Mel*_*lon 0 ruby ruby-on-rails

我有这个非常简单的测试代码:

s="helloCustomer"

is_contain_customer = s=='helloCustomer' || s.include? 'Customer' ? 'YES' : 'NO'
Run Code Online (Sandbox Code Playgroud)

我在代码中的第二行收到错误消息Unexpected tSTRING_BEG.

为什么?

Lar*_*y K 5

除了语法错误(如果语法不明确,你需要围绕方法args的括号),你的代码并不像它那样清晰.

你正在使用||的更高优先级 vs?:使表达式最终返回'YES'或'NO'

为了便于阅读和维护,我建议您添加括号以使意图清晰:

is_contain_customer = 
     (s=='helloCustomer' || s.include?('Customer')) ? 'YES' : 'NO'
Run Code Online (Sandbox Code Playgroud)

此外,如果您正在使用Rails或其他MVC系统,最好使用View层将数据(布尔值)转换为字符串.