From: "headius (Charles Nutter)" Date: 2013-03-15T03:42:14+09:00 Subject: [ruby-core:53422] [CommonRuby - Feature #8088] Method#parameters (and friends) should provide useful information about core methods Issue #8088 has been updated by headius (Charles Nutter). marcandre (Marc-Andre Lafortune) wrote: > +1. > > I plan on proposing a new C API for registering methods, so allow for complete information, including keyword parameters. I was contemplating hacking something together myself, in fact. Would like to see what you're proposing...can you add an issue to CommonRuby? For some background, JRuby's method-registering mechanism is based on Java annotations. For example, String#gsub: @JRubyMethod(name = "gsub", reads = BACKREF, writes = BACKREF, compat = RUBY1_9) public IRubyObject gsub19(ThreadContext context, IRubyObject arg0, Block block) { return block.isGiven() ? gsubCommon19(context, block, null, null, arg0, false, 0) : enumeratorize(context.runtime, this, "gsub", arg0); } @JRubyMethod(name = "gsub", reads = BACKREF, writes = BACKREF, compat = RUBY1_9) public IRubyObject gsub19(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) { return gsub19(context, arg0, arg1, block, false); } We register two separate endpoints for the two supported arities. When these methods are bound into String, we have information on required arguments, optional arguments, and whether there's a rest arg. We don't currently include the actual Java local variables in #parameters output, but we easily could. So I suppose you're thinking about something more like this for MRI? rb_define_method_x(rb_cString, "gsub", rb_str_gsub, 1 /*req*/, 1 /*opt*/, 0 /*rest*/) It would be super nice if it's possible to get actual C parameters, but I don't think you can do that programmatically (i.e. they'd have to be passed in). ---------------------------------------- Feature #8088: Method#parameters (and friends) should provide useful information about core methods https://bugs.ruby-lang.org/issues/8088#change-37610 Author: headius (Charles Nutter) Status: Open Priority: Normal Assignee: Category: Target version: I was wiring up #parameters to work for native methods today when I realized MRI doesn't give very good information about variable-arity native methods: ext-jruby-local ~/projects/jruby $ ruby2.0.0 -e "p ''.method(:gsub).to_proc.parameters" [[:rest]] ext-jruby-local ~/projects/jruby $ jruby -e "p ''.method(:gsub).to_proc.parameters" [[:req], [:opt]] I think MRI should present the same as JRuby here; gsub is obviously not a rest-arg method and you can't call it with less than 1 or more than 2 arguments. JRuby's presenting the right output here. I'm probably going to have to change JRuby to do the less-helpful version so we're compliant and tests pass, but I think the specification of #parameters should be that it presents the JRuby version about rather than the MRI version. -- http://bugs.ruby-lang.org/