------ art_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 *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
------ art_18642_8812556.1226270951390
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<div><div>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.</div>
<div><br></div><div>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]).</div>
<div><br></div><div>Some examples:</div><div><br></div><div>def foo(bar); end</div><div>method(:foo).get_args # t; [[[:bar]], nil, nil]</div><div><br></div><div>def foo(bar, baz, bat ], &blk); end</div><div>method(:foo).get_args # t; [[[:bar], [:baz], [:bat, []]], nil, :blk]</div>
<div><br></div><div>def foo(bar, *args); end</div><div>method(:foo).get_args # t; [[[:bar]], :args, nil]</div><div><br></div><div>def foo(bar, baz *args, &blk); end</div><div>method(:foo).get_args # t; [[[:bar], [:baz, 1]], :args, :blk]</div>
<div><br></div><div>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.</div>
<div><br></div><div>-- </div><div>Yehuda Katz</div><div>Merb Developer | Engine Yard</div><div>(ph) 718.877.1325</div>
</div>
------ art_18642_8812556.1226270951390--