我有以下代码:i ? "x": "y"
但我不想只返回“x”或“y”,我还想将 i 设置为 true 或 false。 我? ("x"; i = false) : ("y"; i = true)
但是不起作用。
最佳答案
(i ? "x" : "y").tap{i = !i}
或
(i = !i) ? "y" : "x"
但如果结果是 XY 情况(我没有在这里写“XY 问题”,因为 OP 没有问任何问题),那么这可能更优雅:
letter = ["x", "y"].cycle
letter.next #=> "x"
letter.next #=> "y"
letter.next #=> "x"
letter.next #=> "y"
...
关于ruby - 多个结果?样式 if 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26562300/