gpt4 book ai didi

ruby - 三元评估是否需要参数化?

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:49 25 4
gpt4 key购买 nike

似乎如果我将代码放入我的三元评估中它会失败,但是放置 truefalse 它会起作用。

这是我的代码:

>test = [nil]
=> [nil]

>test.any? ? puts "AAA" : puts "BBB"
SyntaxError: (irb):16: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
test.any? ? puts "AAA" : puts "BBB"
^
(irb):16: syntax error, unexpected ':', expecting $end
test.any? ? puts "AAA" : puts "BBB"

>test.any? ? true : false
=> false

>test << 1
=> [nil, 1]

>test.any? ? true : false
=> true


>test.any? ? puts "AAA" : puts "BBB"
SyntaxError: (irb):14: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
test.any? ? puts "AAA" : puts "BBB"
^
(irb):14: syntax error, unexpected ':', expecting $end
test.any? ? puts "AAA" : puts "BBB"
^

最佳答案

你需要括号。

>> test.any? ? puts("AAA") : puts("BBB")
BBB
=> nil

你应该避免在内联函数中使用圆括号。

关于ruby - 三元评估是否需要参数化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905960/

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