Using const_missing to do automatic require

E

eastcoastcoder

I'd like to def const_missing to require the appropriate file, without
needing to put require 'filename' in the top of my code.

Something like

const_missing MyMod::MyClass --> require 'my_mod/my_class' ; return
MyMod::MyClass

I got this idea from ActiveRecord.

Any comments / advice on using this for general programmign? Any
reason not to? I think it would clean up a lot of the require lines,
and make refactoring (and moving things around) easier, without really
detracting.
 
E

eastcoastcoder

Sorry - that's something else.

Rails' automatically loads the file just by mentioning a class of that
name - you don't need to configure it anywhere.

autoload allows you to specificy once which file to load for a class.
It's no shorter than just requiring it - I guess it's a performance
boost.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top