Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a return statement break the conditional operator?

Experimenting with the conditional operator in ruby,

def nada
  false ? true : nil
end

def err
  false ? true : raise('false')
end

work as expected but

def reflection
  false ? true : return false
end

produces a syntax error, unexpected keyword_false, expecting keyword_end

def reflection
  false ? true : return(false)
end

and attempted with brackets syntax error, unexpected tLPAREN, expecting keyword_end

yet

def reflection
  false ? true : (return false)
end

works as expected, and the more verbose if...then...else...end

def falsy
  if false then true else return false end
end

also works as expected.

So what's up with the conditional (ternary) operator?

like image 777
Alec Wenzowski Avatar asked Nov 13 '11 14:11

Alec Wenzowski


People also ask

Which operator returns a true value if the condition is false?

This conditional operator will return a true value if the given condition is false. This conditional operator will return a true value if both the given conditions are true and Boolean. This conditional operator will return a true value if either of the given conditions is true and Boolean.

Does this statement require any conditional statements?

This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and return the control from where it was called. The return statement may or may not return anything for a void function, but for a non-void function, a return value is must be returned.

What are conditional operators in C?

Conditional Operator in C | How Does Conditional Operators Work in C? If we break these two words then the operator means a symbol that operates on some value while a condition is something that can be applied to the operator to perform some specific operations.

What is the difference between an operator and a condition?

If we break these two words then the operator means a symbol that operates on some value while a condition is something that can be applied to the operator to perform some specific operations. The conditional operator has two value and it shows the output value based on the given conditions.


2 Answers

You can use it like this, by putting the entire return expression in parentheses:

def reflection
  false ? true : (return false)
end

Of course, it does not make much sense used like this, but since you're experimenting (good!), the above works! The error is because of the way the Ruby grammar works I suppose - it expects a certain structure to form a valid expression.

UPDATE

Quoting some information from a draft specification:

An expression is a program construct which make up a statement (see 12 ). A single expression can be a statement as an expression-statement (see 12.2).12

NOTE A difference between an expression and a statement is that an expression is ordinarily used where its value is required, but a statement is ordinarily used where its value is not necessarily required. However, there are some exceptions. For example, a jump-expression (see 11.5.2.4) does not have a value, and the value of the last statement of a compound-statement can be used.

NB. In the above, jump-expression includes return among others.

like image 200
Zabba Avatar answered Oct 11 '22 06:10

Zabba


I think this is all related to the ruby parser.

  • ruby parses return as the else-expression of the ternary operator
  • ruby is then surprised when it finds false instead of end
  • wrapping return false in parentheses causes ruby to interpret the entire thing as the else-expression
  • return(false) doesn't work because ruby is still trying to interpret just the return part as the else-expression, and is surprised when it finds a left-parenthesis (updated)

Note: I don't think this is a great answer.

A great answer could, for example, explain the parse errors with reference to the ruby grammar.

like image 33
Matt Fenwick Avatar answered Oct 11 '22 05:10

Matt Fenwick