-2

I am using ruby 2.1.5, facing some problem with ternary operator

Syntax error

request.xhr?  ? render :json => "success"  : redirect_to index_url

working

request.xhr?  ? render(:json => "success") : redirect_to(index_url)

Can some please explain How its works and why above one not working? Thanks in advance

Ram Kumar
  • 94
  • 7
  • I may be wrong but I suspect there's an ambiguity for the parser knowing where to split things, adding parentheses remove this ambiguity giving what is a parameter to what. – Tensibai May 06 '15 at 07:47

1 Answers1

0

When you use the shorthand syntax (without brackets), ruby expects everything until the end of the line to be parameters to your method. So your "syntax error" example is understood as:

request.xhr?  ? render(:json => "success"  : redirect_to index_url)

which is obviously wrong.

Piotr Kruczek
  • 2,384
  • 11
  • 18