From: Joel VanderWerf Date: 2006-09-13T02:59:06+09:00 Subject: Re: Reflection and files MonkeeSage wrote: > Gavin Kistner wrote: >> Well, here's one terrible idea. (It's 'terrible' because it's brittle, >> and only works if no other files defining new classes have been defined, >> and will break if a new Ruby release defines a new class.) > > How about: > > def get_class_from_file(file) > consts = Object.constants > require file > return (Object.constants - consts)[0] > end > > p get_class_from_file('/path/to/some_file.rb') Doesn't work if file requires some other file (such as a ruby std lib file). Also, not thread safe. Also, the class may be defined as class Foo::Bar end and it will not show up in Object.constants. Also, there might be other constants: K=1 class Foo; end It's a hard problem in ruby... -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407