From: Matt Todd Date: 2008-11-10T10:26:34+09:00 Subject: [ruby-core:19764] Re: Proposal: Method#get_args ------=_Part_36543_17586811.1226280536119 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline +1 (or is that frowned upon?) I definitely think that this would be a great compliment to arity inspection... Matt On Sun, Nov 9, 2008 at 5:46 PM, Yehuda Katz wrote: > I'd like to propose a way to introspect into the arguments of a method > object. Merb uses this feature, which is already available via ParseTree in > 1.8.6, via compile-time in Rubinius, and via internal structures in JRuby. > I'd like to formalize the API so it can be added to 1.9, which cannot use > ParseTree. > > The API would return a tuple representing the arguments. The first element > of the tuple is an array of the required and optional arguments. Required > arguments are a single-element tuple containing a symbol representing the > argument ([:foo]). Optional arguments with pure-literal defaults are > represented as a two-element tuple containing a symbol representing the > argument and the literal ([:foo, 1]). In the current implementations of this > API in 1.8, JRuby and Rubinius, non-literal defaults return nil. Instead, we > could create a constant that got returned (something like Method::OPTIONAL, > [:foo, OPTIONAL]). > > Some examples: > > def foo(bar); end > method(:foo).get_args #=> [[[:bar]], nil, nil] > > def foo(bar, baz, bat = [], &blk); end > method(:foo).get_args #=> [[[:bar], [:baz], [:bat, []]], nil, :blk] > > def foo(bar, *args); end > method(:foo).get_args #=> [[[:bar]], :args, nil] > > def foo(bar, baz=1, *args, &blk); end > method(:foo).get_args #=> [[[:bar], [:baz, 1]], :args, :blk] > > This could be handy for generating APIs for Ruby classes. In Merb, we use > this feature to convert inbound requests that list their parameters > (essentially) as a hash, into a Ruby method call. The benefit is the Merb > actions can be called from other actions as regular method calls. > > -- > Yehuda Katz > Merb Developer | Engine Yard > (ph) 718.877.1325 > ------=_Part_36543_17586811.1226280536119 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline +1

(or is that frowned upon?)

I definitely think that this would be a great compliment to arity inspection... 

Matt



On Sun, Nov 9, 2008 at 5:46 PM, Yehuda Katz <wycats@gmail.com> wrote:
I'd like to propose a way to introspect into the arguments of a method object. Merb uses this feature, which is already available via ParseTree in 1.8.6, via compile-time in Rubinius, and via internal structures in JRuby. I'd like to formalize the API so it can be added to 1.9, which cannot use ParseTree.

The API would return a tuple representing the arguments. The first element of the tuple is an array of the required and optional arguments. Required arguments are a single-element tuple containing a symbol representing the argument ([:foo]). Optional arguments with pure-literal defaults are represented as a two-element tuple containing a symbol representing the argument and the literal ([:foo, 1]). In the current implementations of this API in 1.8, JRuby and Rubinius, non-literal defaults return nil. Instead, we could create a constant that got returned (something like Method::OPTIONAL, [:foo, OPTIONAL]).

Some examples:

def foo(bar); end
method(:foo).get_args #=> [[[:bar]], nil, nil]

def foo(bar, baz, bat = [], &blk); end
method(:foo).get_args #=> [[[:bar], [:baz], [:bat, []]], nil, :blk]

def foo(bar, *args); end
method(:foo).get_args #=> [[[:bar]], :args, nil]

def foo(bar, baz=1, *args, &blk); end
method(:foo).get_args #=> [[[:bar], [:baz, 1]], :args, :blk]

This could be handy for generating APIs for Ruby classes. In Merb, we use this feature to convert inbound requests that list their parameters (essentially) as a hash, into a Ruby method call. The benefit is the Merb actions can be called from other actions as regular method calls.

-- 
Yehuda Katz
Merb Developer | Engine Yard
(ph) 718.877.1325

------=_Part_36543_17586811.1226280536119--