On Friday 06 November 2009 04:08:45 am Robert Klemme wrote:
> 2009/11/5 Don French <dhf0820 / gmail.com>:
> > Ok, that gives me the list of the files. how do I execute a known
> > class method call it self.identify in each of them, these are all of
> > the same class. That is still the part I do not understand.
>
> I typically find it easier to use a registration process, e.g.
>
> in mod1.rb:
>
> class X
>   def self.identify; "foo"; end
> end
>
> ::MODULES << X

I usually like to do that implicitly. For example, all these files probably 
have something in common, or they wouldn't be called this way. Make them 
either inherit from a common ancestor or include a common module. Here's how 
to do it with a module:

require 'set'
module Foo
  Children = Set.new
  def self.included klass
    Children << klass
  end
end

This is just as easy to do with classes -- just use inherited instead of 
included.

And then, to borrow an earlier example:

result = nil
klass = Foo::Children.find {|klass|
  result = klass.send :some_method
}


Another possibility would be to enforce a strict naming convention -- for 
example, if there's a foo.rb, you assume it contains a Foo class. You could do 
something like:

require 'extlib'
classes = filenames.map{|n| Object.const_get n.camel_case}

...depending on what library you use to get the camel_case method. Or you 
could do it yourself with a simple regex.