1

似乎如果我将代码放入我的三元评估中,它会失败,但放置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"
                        ^
4

1 回答 1

3

你需要括号。

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

您应该避免在内联函数中进行无括号调用。

于 2012-10-16T00:40:09.023 回答