--bcaec5016159a6089404ae00244e
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Sep 28, 2011 at 7:33 AM, Jeroen van Ingen <jeroeningen / gmail.com>wrote:

> I have a list of ruby files. I would like to create objects from all
> classes using eval(classname)
>
> My filelist looks something like:
> nl/foo_nl_bar.rb #class NL::FooNLBar
> en/bar_en_foo.rb #class EN::BarENFoo
>
> For now my solution is as follows:
> File.readlines(rubyfile).find_all{|line|
> line.start_with?("class")}.collect{|file|
> file.split("<")[0].gsub(/class/, "")}.map(&:strip)
>
> IMHO this is ugly
>
> I tried doing it with Module.constants. It returns the classnames, but
> the namespace is missing.
> Using String.camelize.constantize returns BarEnFoo. So here the
> namespace is also missing and the camelize doesn't work.
>
> Doe anyone know a cleaner solution?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
If you follow common conventions, you can infer class names from filenames.

What problem are you trying to solve? What if, instead of scraping files and
evaling strings, you had the classes register themselves with whatever needs
these instances? Here is an example:

This class needs to know about all of my interfaces
https://github.com/JoshCheek/ttt/blob/1b10b6ac18ccf066d992abdc12ec6e43acf55f08/lib/ttt/interface.rb

So when they are defined, they tell the class that they exist
https://github.com/JoshCheek/ttt/blob/1b10b6ac18ccf066d992abdc12ec6e43acf55f08/lib/ttt/interface/cli.rb#L7
https://github.com/JoshCheek/ttt/blob/1b10b6ac18ccf066d992abdc12ec6e43acf55f08/lib/ttt/interface/limelight.rb#L7

--bcaec5016159a6089404ae00244e--