22 Mar '14, 8pm
Explains how to write #Ruby Driver Test code to enforce the number of Req'd Method arguments
Is there a way to return the number of required arguments, the number of optional arguments and whether or not a splat is present for a method? The closest I've managed to find is Method.arity which will return a number which indicates the number of required arguments and the *presence* of optional arguments only (but in a non-obvious way): With only required arguments, it behaves as expected: >> def foo(a, b); end => nil >> method(:foo).arity => 2 When optional arguments are introduced however: >> def foo(a, b=2); end => nil >> method(:foo).arity => -2 Documentation reveals that when a method has optional arguments, the arity is -n - 1 where n is the number of required arguments (which seems a little arbitrary, is there a historical reason for this?). However, adding new optional arguments and splats makes no difference to the arity: >> def foo(a, b=2, *c); end => nil >> ...