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.
arity. There is no secret second parameter, there's just an operation that moves the first element intox, discarding the rest. Rust has a similar operation.proc { |(x,y,z)| puts x, y, z }also has an arity of 1.RubyVM::AbstractSyntaxTree(or a Ruby parser) count?