[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using const_missing to do automatic require

eastcoastcoder

3/15/2006 8:28:00 PM

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.

3 Answers

Trans

3/15/2006 9:40:00 PM

0

It's already built into Ruby. Try 'ri autoload'.

T.

James Gray

3/15/2006 9:51:00 PM

0

On Mar 15, 2006, at 3:43 PM, Trans wrote:

> It's already built into Ruby. Try 'ri autoload'.

Holy cow, I never knew that. Thanks for the info!

James Edward Gray II


eastcoastcoder

3/19/2006 4:45:00 AM

0

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.