Forum: Ruby Using const_missing to do automatic require

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3b786a23390451fa67108781b0c8fed?d=identicon&s=25 unknown (Guest)
on 2006-03-15 21:30
(Received via mailing list)
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.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-03-15 22:44
(Received via mailing list)
It's already built into Ruby. Try 'ri autoload'.

T.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-15 22:53
(Received via mailing list)
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
F3b786a23390451fa67108781b0c8fed?d=identicon&s=25 unknown (Guest)
on 2006-03-19 07:29
(Received via mailing list)
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.
This topic is locked and can not be replied to.