I am curious about having a trailing comma in a block in Ruby.
For example:
[[1, 2], [3, 4]].collect { |x, | x }
# returns [1, 3]
It's as if there is an optional argument after the first argument.
However:
(proc { |x, | x }).arity
# returns 1
If arity is 1 then the array should not be decomposed across X.
Checking (proc { |x, | x }).parameters
gives no hint that this any "secret" second parameter.
Are there methods of introspection to tell that proc { |x, | }
is
different from proc { |x| }
?
I understand the basics of decomposition etc, and I can see that a trailing comma effectively creates a "secret" parameter in the since that |x, | and |x, _| work the same.
But what I am surprised at is there is no introspective way to find that the trailing comma is there, short of getting into the AST. It is just surprising.