有人可以在这里解释冒号的功能吗?
Can someone explain the function of colon here?
谁能解释一下 :
在这种情况下的作用?
def group_by_marks(marks, n)
marks.group_by {|key, value| value <n ? "Failed" : "Passed"}
end
这是一个三元条件。冒号表示如果value
大于等于n
,则使用"Passed"
.
value < n ? "Failed" : "Passed"
相当于
if value < n then "Failed" else "Passed" end
它是三元运算符。如果value < n
为真,则此块的return值为"Failed"
;如果 value < n
为假,则 return "Passed"
.
这个冒号可以简单的看成"either this or that".
谁能解释一下 :
在这种情况下的作用?
def group_by_marks(marks, n)
marks.group_by {|key, value| value <n ? "Failed" : "Passed"}
end
这是一个三元条件。冒号表示如果value
大于等于n
,则使用"Passed"
.
value < n ? "Failed" : "Passed"
相当于
if value < n then "Failed" else "Passed" end
它是三元运算符。如果value < n
为真,则此块的return值为"Failed"
;如果 value < n
为假,则 return "Passed"
.
这个冒号可以简单的看成"either this or that".