Ruby包括?在if语句中

时间:2012-02-16 19:19:16

标签: ruby

为什么会导致语法错误“语法错误,意外的keyword_end,期待$ end”?:

if "test".include?"te" || "test".include?"fail"
  puts "true"
end

以下作品:

fail = "test".include?"fail"

if "test".include?"te" || fail
  puts "true"
end

4 个答案:

答案 0 :(得分:5)

将括号与include?个参数一起使用。

if "test".include?("te") || "test".include?("fail")
  puts "true"
end

答案 1 :(得分:5)

另一种解决方案:替换运营商“||”使用优先级较低的“或”,以便省略括号:

if "test".include?"te" or "test".include?"fail"
  puts "true"
end

答案 2 :(得分:3)

您必须在第二个参数周围使用括号。

if "test".include?("te") || "test".include?("fail")
  puts "true"
end

if "test".include? "te" || ("test".include? "fail" )
  puts "true"
end

答案 3 :(得分:1)

if "test".include?("te") || "test".include?("fail")
  puts "true"
end