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.
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.
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.
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.
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.
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.
I think this is all related to the ruby parser.
return
as the else-expression of the ternary operatorfalse
instead of end
return false
in parentheses causes ruby to interpret the entire thing as the else-expressionreturn(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With