From: Francis Hwang Date: 2004-11-12T04:11:36+09:00 Subject: Re: Thinking About Java Interfaces In Ruby On Nov 11, 2004, at 1:28 PM, Jamis Buck wrote: > True, and I considered that (the approach, not the PhD :-P). However, > most of the time, the interface that is desired is only a small subset > of the methods published by the object... > > Also, things are further complicated by the fact that you can't just > look at a service definition and determine its interface by > reflection. This is because the service definition (in Needle) is what > is returned by executing a block, and I'd like to be able to get the > interface information without having to execute the block (and thereby > actually instatiate the object). > > I've considered having service declarations explicitly name the > methods they export, but that's where things just become too much > work. Besides, "interface injection" just doesn't feel very Rubyish, > especially since interfaces in Ruby can be changed on the fly, and may > even differ from one object to the next, even though they share the > same instantiating class. > I assume you don't want to instantiate the service because instantiation is expensive? How about just instantiating it once, getting the methods, and then saving them for later? (what's below is not tested, I'm just thinking in code ...) service_class = SomeReallyExpensiveService service = service_class.new my_methods = service.methods File.open( "~/.needle/#{ service_class.name }.methods.yaml", "w" ) { |f| YAML.dump( my_methods, f ) } Then whenever Needle is wrapping that service, you can look for the YAML file to see if you've got the methods pre-processed. It's like post-compilation! Holy crap, do I love reflectivity. Anyway, it's true that this all falls apart if you're adding per-instance methods to a service. Personally, I really don't like adding per-instance methods: For all my loosey-goosy typeless talk, I'm sort of a chicken about that stuff and I like all my objects of one class to respond to the exact same set of methods. But that's just me. F.