I like method parameters enclosed in parenthesis, this is some Pascal nostalgia. When cleaning up code, if I find a method parameters without it I enclose them immediately.
Today it caused my working code throwing errors although my syntax looks okay according to the documentation.
Kernel.raise’s documentation has this format:
(Object) raise(exception[, string [, array]])
These are all working:
> raise TypeError
TypeError: TypeError
> raise (TypeError)
TypeError: TypeError
> raise "Error message"
RuntimeError: Error message
> raise ("Error message")
RuntimeError: Error message
But the enclosed version of the next throws syntax error:
> raise TypeError, "Error message"
TypeError: Error message
> raise (TypeError, "Error message")
SyntaxError: unexpected ')', expecting $end
I can live without it, I just want to know why this ends with an error.
You probably already know that in idiomatic Ruby one would never insert a space between the end of a method and an argument list in parenthesis. Some style guides explicitly forbid it.
There’s a pragmatic reason too.
You’ll encounter errors calling any method this way, not just
Kernel.raise.I’m not familiar with Ruby internals, but I would imagine the reason for this is that when a space precedes the argument list, Ruby is expecting the “no-parens” style. So of course this works:
Presumably Ruby is trying to evaluate the contents of each parenthesized expression before passing the result to the method. I’d speculate that writing
raise (TypeError, "Error message")is asking Ruby to evaluate justTypeError, "Error message", which of course fails.