gpt4 book ai didi

Ruby 三元 - 警告 : string literal in condition

转载 作者:数据小太阳 更新时间:2023-10-29 07:43:15 24 4
gpt4 key购买 nike

此代码按预期工作:

if phrase.last.eql? "?" ? true : false
true
else
false
end

但是这段代码使用了 Ruby 三元运算符:

phrase.last.eql? "?" ? true : false

出现以下错误:

warning: string literal in condition

我是否需要以某种方式转义 "?"

最佳答案

没有括号,ruby 将其解释为

phrase.last.eql?( "?" ? true : false )

它解释了消息“警告:条件中的字符串文字”。

要解决此问题,请在参数上使用括号:

phrase.last.eql?("?") ? true : false

当然,在这种情况下使用三元运算符是多余的,因为这与简单地相同

phrase.last.eql?("?")

关于Ruby 三元 - 警告 : string literal in condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com