Ruby ternary operator and method call

隐身守侯 提交于 2019-12-03 00:49:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30070319/ruby-ternary-operator-and-method-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!