From: Yehuda Katz Date: 2008-11-10T07:46:51+09:00 Subject: [ruby-core:19759] Proposal: Method#get_args ------=_Part_18642_8812556.1226270951390 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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_18642_8812556.1226270951390 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
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_18642_8812556.1226270951390--