From: Robert Klemme Date: 2004-10-15T18:34:28+09:00 Subject: Re: Querying class attributes "DaZoner" schrieb im Newsbeitrag news:ckmgi5$r3k$1@news.doit.wisc.edu... > > I'm creating a program that allows anonymous plugins to be defined. Since > the user can define their own plugins I want to check that they've defined > them properly. What I can't figure out is how to check the plugin's > correctness without instantiating an instance of it first thus making > checking impossible. I'm sure there is an easy way to do this but I don't > know what it is. > > In the example below I want to check that the plugin has defined a > constructor that takes 4 parameters. I seem to only be able to check the > instance. How can I do it? def test_class(cl) raise "Wrong arity" unless cl.instance_method(:initialize).arity == 4 end Kind regards robert > > plugin = Class.new do > def initialize(a,b,c,d) > end > end > instance = plugin.new(0,0,0,0) > > def test(object,name,method) > print name," responds to ",method.to_s > if object.respond_to?(method,true) > print " true\n" > print " arity ",object.method(method).arity,"\n" > else > print " false\n" > end > end > > test(plugin,"plugin",:new) > test(plugin,"plugin",:initialize) > test(instance,"instance",:new) > test(instance,"instance",:initialize) > > > ## resulting output > > plugin responds to new true > arity -1 > plugin responds to initialize true > arity -1 > instance responds to new false > instance responds to initialize true > arity 4 > >