In a helper I’ve got the code:
signed_in? ? link_to("Sign out", signout_path, :method => :delete) : link_to("Sign in", signin_path)
However the same thing can be written:
if(signed_in?)
link_to 'Sign Out', signout_path, :method => :delete
else
link_to 'Sign In', signin_path
end
In the second case, the parens for the method call aren’t needed, in the first case they are. Why is that?
You don’t need parens when there is no ambiguity in the function calls. For instance:
and something like
foo 1, bar 2 would be foo(1,bar(2)), but what aboutfoo 1, bar 2, 3that could go to eitherfoo(1,bar(2,3))orfoo(1,bar(2),3)So in the last example you would need to put in the parens yourself to tell ruby what you actually want it to do.
Also, if you ever want to call a method on the return value of the method you’ll need parens too
i.e
foo(1,2).bar