From: Ryan Leavengood Date: 2005-04-30T03:49:08+09:00 Subject: Re: ruby static typing Lyndon Samson wrote: > > Although they were GUI specific, Remember VBX/OCX, how sweet it was > that you could drop in 3rd party functionality into a MS IDE and the > IDE would immediatly know what the interface to be presented to the > programmer was. > > Imagine trying to do the same in Ruby or another softly typed > language, A whole lot of framework code would need to be written and > maintained ( human written unit tests vs compiler enforced type > checking ). This is probably a naive implementation of what you described above (and maybe I've misinterpreted you), but it still isn't all that complicated: class Class def exposed_methods @exposed_methods ||= [] end def attr_exposed(*method_symbols) (@exposed_methods ||= []).concat method_symbols end end class MyComponent attr_exposed :do_this, :do_that def do_this "do this!" end def do_that "do that!" end end class ComponentUser def ComponentUser::show_component_methods(component_class) puts "Exposed methods for #{component_class.name}:" obj = component_class.new component_class.exposed_methods.each do |m| arity = obj.method(m).arity puts "- #{m}(#{arity >= 0 ? arity : 'variable number of'} arguments)" end end end class String attr_exposed :[], :slice, :each_byte end if $0 == __FILE__ ComponentUser.show_component_methods(MyComponent) ComponentUser.show_component_methods(String) ComponentUser.show_component_methods(Array) end This way you can limit clients to what methods they can use based on the exposed_methods. Of course the clients need to play along nicely, which Ruby can't necessarily force them to do. But this begs the question, isn't Class#instance_methods(false) essentially the same as this exposed_methods in most cases? Of course after writing this I realized you still don't know what the arguments mean. But that is what ri is for. Plus even with Java or other component frameworks you still need some information external to the component to learn how to use it, even if you know what the static types of the arguments are. The results from the above exposed_methods call could be linked with ri in a component system to ease this. Hmmm, not a bad idea... Ryan