From: Charles Oliver Nutter Date: 2008-11-14T15:30:44+09:00 Subject: [ruby-core:19928] Re: {Proc,Method}#parameters (Re: Proposal: Method#get_args) Nobuyoshi Nakada wrote: > Hi, > > At Thu, 13 Nov 2008 18:44:53 +0900, > Brian Candler wrote in [ruby-core:19915]: >> That does give me an idea. Since "?" is not legal in a variable name, then a >> very simple option could be >> >> def foo(a, b, c=123, d=nil, *e, &blk) >> >> => ["a", "b", "?c", "?d", "*e", "&blk"] > > It's simplest. The original proposal also asked for literal defaults to be present. I'm not sure that's possible with this version. I think I like a version that doesn't require examining/mutating the string as well. With this: [[:req, 'a'], [:opt, 'b', 1]] Building a list of params is a simple matter of... params.each do |p| case p[0] when :req # do something with p[1] ... ...and creates no new objects at all. With the encoded string version, it would require: params.each do |p| case p[0] # new single-char string when '?' name = p[1..-1] # new string has to be sliced out ... else # else case to handle required args with no leading char... # == slowest possible processing for most common case name = p end end The first version is lighter-weight and the cases can be ordered to favor the common case of required args. >> define_method(:bar) { |a,(b,c)| } >> >> => ["a", ["b", "c"]] > > Is it really necessary? I have doubts myself, but I won't vote one way or another. - Charlie