From: "Jesús Gabriel y Galán" Date: 2013-06-06T21:55:12+09:00 Subject: Re: Playing with method_missing -- argument list On Thu, Jun 6, 2013 at 2:15 PM, Tamara Temple wrote: > > In https://gist.github.com/tamouse/5719507 , I've been playing around > with using Struct, Enumerable, Comparable and lastly method_missing, and > here's where I got stuck. > > At lines 67-69 ("This is going to be fun"), I collect the instance > methods defined in each of Array, Hash, and String, to be able to pass > them along to each of the components of MyClass. > > The problem now comes, how many arguments do I pass along as well? > > My test example, #keys from Hash, shows my problem. #keys takes no > arguments, but even if the argument list is empty, it's an empty Array, > which of course is something. In some contexts this might be fine, but > in others, not so much. > > So, is there a way to detect the number of arguments a method takes? > > Or, would it be better to detect the size of args at that point and send > zero, one or many to the attribute's method? Thinking something like: > > def check_size(args) > case args.size > when 0 ; nil > when 1 ; args.first > else args > end > > (only maybe not so weak a method name) > > AND, is this already a standard function/idiom? > I think you should be calling send with *args: 2.0.0p195 :001 > h = {} => {} 2.0.0p195 :002 > args = [] => [] 2.0.0p195 :003 > h.send(:keys, *args) => [] 2.0.0p195 :004 > h = {:a => 3} => {:a=>3} 2.0.0p195 :005 > h.send(:keys, *args) => [:a] Jesus.