Mike said:
Is this the norm? I would have thought it would be a real problem
parsing big frameworks with myriads of classes everytime you want to
make a simple call.
Once a library has been require'd once, then the fact that this has been
done is recorded (in variable $LOADED_FEATURES), and so it won't be
loaded again. If you want to check this out: require returns 'true' if
it had to load something, and 'false' if it didn't.
Autoload goes one stage further: it won't load the file unless the
constant doesn't exist.
-------------------------------------------------------- Kernel#autoload
autoload(module, filename) => nil
------------------------------------------------------------------------
Registers _filename_ to be loaded (using +Kernel::require+) the
first time that _module_ (which may be a +String+ or a symbol) is
accessed.
autoload
MyModule, "/usr/local/lib/modules/my_module.rb")
That is: the first time you try to do MyModule.foo, the file is
required, which (presumably) defines MyModule. Since it now exists, the
next call to MyModule.bar won't touch any source files.
Is there a way of keeping a parsed script cached eg
for a web site that is handling hundreds of users doing the same sorts
of things eg browsing product lists?
As I say, once the file has been loaded, it's loaded.
The only problem comes if you quit the process and restart it, as then
it obviously has to load everything again from scratch. I wrote
something which can help with that:
http://github.com/candlerb/snailgun