On Oct 11, 2007, at 01:30 , Charles Oliver Nutter wrote: > Eric Hodel wrote: >> On Oct 10, 2007, at 22:44 , Charles Oliver Nutter wrote: >>> It seems like a number of methods have unexpected arities. For >>> example, Array#[] >>> >>> $ ruby -e "puts Array.method(:[]).arity; x = [1,2,3]; x[]" >>> -1 >>> >>> I've been running into a number of these. We have been trying to >>> get all methods and arities matching in JRuby, but these numbers >>> don't seem to make sense. Should they be corrected in Ruby? >> The -1 arity exposes the argument checking implementation, which >> is done manually for -1 methods. On the C side, the -1 and -2 >> arities tell ruby how to call the C function properly. Adding the >> Ruby arity to them would require instrumenting every method, but >> non-stdlib extensions would still lie. >> Is it a big deal? Its easy to make a method that lies about its >> arity: >> def liar(a, b, c, d, e, f, g, h) >> raise ArgumentError, "wrong number of arguments (0 for 1)" >> end > > Ideally we'd be able to use (and present) the actual arity to do > automated arity checking in most of these cases, rather than have > to report one arity and actually enforce another. It seems pretty > messy your way. The information is there, but I don't see a way to automatically make sure it is correct for MRI/YARV. I don't think this is an area where you need to maintain compatibility. > What good does Method#arity do if it isn't the actual arity being > enforced? From the Ruby side, it doesn't help to know the C arity > number. In JRuby now, every method stores required arg count, > optional arg count, and whether rest args or a block arg are > accepted, and those values are used to calculate the appropriate > arity. I agree that its not helpful for the C methods. It would be easy to fix, but I don't know how to automate it so that it is always correct. > Is it not possible to get -2, -3, -4 etc arities from Method#arity? Currently, for ruby methods you can get -2, -3, etc, for C methods you can only get -1. See method_arity() in eval.c -- Poor workers blame their tools. Good workers build better tools. The best workers get their tools to do the work for them. -- Syndicate Wars