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.
You can use it like this, by putting the entire
returnexpression in parentheses: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:
NB. In the above, jump-expression includes
returnamong others.