0

Can someone explain what : does in this case?

def group_by_marks(marks, n)
    marks.group_by {|key, value| value <n ? "Failed" : "Passed"}
end
sawa
  • 165,429
  • 45
  • 277
  • 381
Ken Saluda
  • 31
  • 1
  • 6

2 Answers2

4

That is a ternary condition. The colon says that if value is greater than or equal to n, use "Passed".

value < n ? "Failed" : "Passed"

Equivalent to

if value < n then "Failed" else "Passed" end
sawa
  • 165,429
  • 45
  • 277
  • 381
Lawrence Johnson
  • 3,924
  • 2
  • 17
  • 30
2

It's ternary operator. If value < n is true, then the return value of this block is "Failed"; if value < n is false, return "Passed".

You can simply regard this colon as "either this or that".

sawa
  • 165,429
  • 45
  • 277
  • 381
Troy Liu
  • 81
  • 4