From: TRANS Date: 2007-03-22T10:52:56+09:00 Subject: Re: Ruby Method Signatures (was Re: Multiton in standard library) On 3/21/07, Jos Backus wrote: > On 3/19/07, TRANS wrote: > > > >Which reminds me, it would be nice of Method could return a > >representation of a methods signature. I've run into this need > >elsewhere too. > > This would be nice for some kinds of tracing. We have a Perl Trace library at > work which prints the method's arguments, and is used like this: > > su foo { > my($arg1, $arg2) = @_; > Trace::begin(@_); > ... > } > > No easy way to do this in Ruby beyond > > Trace.begin(arg1, arg2) > > Instead I'd like to be able to write > > Trace.begin(__argv__) I'm a little confused, What's output of this? This maybe completely aside, but the above reminds me that with 'this', there could be: def(a,b) Trace.begin(*this.arguments) end Basically args irregardless of the the actual signature. As for the signature, it would have to be a special class of object that has the qualities of Ruby's arguments -- the ordered list, the default params and etc. With that object a few things could be queried, such as arity, maybe a list of parameter names, whether it has a * parameter or a &block parameter, etc. Importantly it could output a string representation or the args (whether with the original names or not). Eg. def a(x,y) this.signature.to_s #=> "x,y" -or- "a1,a2" A bonus would be to use the arguments in place of actual arguments when defining a method. def foo(x) end def bar(*=method(:foo).signature) p x # or p a1 end bar(1) #=> 1 The *= is needed b/c there has to a special way to indicate that. T.