If I load x.rb
, then all the classes in that file are loaded. Is it possible to check and see what classes are being used and load those only?
Assuming x.rb contains both Hello and Goodbye classes, and my program only uses the Hello Class, is it possible to load only the Hello Class?
Happy with a script that checks the document, and outputs an .rb that has only the Hello Class and the code that uses it... Would be an interesting github project if doesn't exist, but I think it's out of my skillset atm.
NOTE: I misread part of the question and the other part somehow evaded me... I must have been multitasking big time. Anyway, this answer only answers half of the question, and incorrectly; just saying.
Okay, here's a possibly expensive way, but try something like this:
Now for a cleaner and way, you could use sockets. I'll get an example of this up.
When classes are defined in their own separate file, you could use the
autoload
¹² method:When you write
Hello
, you are actually accessing theHello
constant.autoload
usesconst_missing
to automatically require a file if the constant is not defined.Note that we are still dealing with files here. The contents of
x/hello.rb
will simply be read and evaluated. That code can run any operation. It canrequire
other files. It could define a million other classes.That's because source code is really just text. This is especially true for interpreted languages. For example, in Java, you can usually only declare one public type per "compilation unit". In Ruby, there is no such thing.
¹ Matz strongly discourages the practice
² Ruby Inside article on
autoload