I am new to ruby, know conditional statements, I tried to below:

marks_hash
.group_by {
  |name, marks|
  marks >= pass_marks? "Passed": "Failed"
}
enter code here

This innocent looking code, gave below error:

syntax error, unexpected tSTRING_BEG, expecting do or '{' or '('

1 Answers

-2
Manohar Reddy Poreddy On Best Solutions

Answering my own question, as part of SO's share your knowledge

The issue was in below line:

  marks >= pass_marks? "Passed": "Failed"

it should be:

  marks >= pass_marks ? "Passed": "Failed"

Note the ? is attached to pass_marks in the 1st example, but has space before ? in the second example.

The difference is, in pass_marks?, pass_marks is treated as a method, in the second example, pass_marks is created as a variable in ternary operator condition.

Even better is having brackets to indicate the order of execution, then space before ? won't matter for this case:

  (marks >= pass_marks)? "Passed": "Failed"