Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
C 有短的条件分支运算符。
int a = 1 < 2 ? 3 : 4;
Ruby 中的等价物是什么?
Ruby 也有三元运算符,你可以用同样的方法来做。
a = 1 < 2 ? 3 : 4
a = true ? 'a' : 'b' #=> "a" b = false ? 'a' : 'b' #=> "b"
您也可以使用整个if语句,因为它也是一个表达式:
if
a = if 1 < 2 then 3 else 4 end
甚至:
a = if 1 < 2 3 else 4 end