Load only what classes are being used in Ruby?

Go To StackoverFlow.com


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.

2012-04-03 22:31
by Mr. Demetrius Michael
No, that's not possible. Source files are loaded and evaluated in their entirety. Why not use separate files - Niklas B. 2012-04-03 22:45
The time and memory you and the computer expend on exclusively loading the Hello class will probably be greater than simply loading Goodbye along with it - benzado 2012-04-03 22:56


When classes are defined in their own separate file, you could use the autoload¹² method:

autoload :Hello,   'x/hello'
autoload :Goodbye, 'x/goodbye'

When you write Hello, you are actually accessing the Hello constant. autoload uses const_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 can require 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

2012-04-03 22:54
by Matheus Moreira


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:

$ cat definer.rb
class Foo
  def greet(person)
    puts "Hello, #{person}!"
Bar = Class.new

$ cat finder.rb
$LOAD_PATH << File.dirname "."
before = Object.constants
require 'definer'
after = Object.constants
print (before - after).join(" ")

$ cat looker.rb
differences = `ruby finder.rb`.split(" ")
puts "The following constants will be defined when finder.rb is loaded: #{differences.join(", ")}"

Now for a cleaner and way, you could use sockets. I'll get an example of this up.

2012-04-03 22:52
by Jwosty
I don't see the connection to the question - Niklas B. 2012-04-03 23:22
It will print all constants that definer.rb defines, without actually doing so in the main program - Jwosty 2012-04-03 23:29
Wait a minute... I think I posted to the wrong question... facepalmJwosty 2012-04-03 23:31
Quote from the question: "Is it possible to check and see what classes are being used and load those only?" This answer addresses neither of these points, IMH - Niklas B. 2012-04-03 23:31
I don't know, I mainly wanted to prevent you from inventing another, possibly more complex example involving sockets (?) because you'd probably only waste your time with it.. I don't think it's a sensible question, after al - Niklas B. 2012-04-03 23:33
Oops, I only really saw the first part... Disreguard this answe - Jwosty 2012-04-03 23:34
Yeah, actually, I was debugging a program that would help nobody coming to this question... Thanks for saving me, haha - Jwosty 2012-04-03 23:35
No worries, man :) Keep up the good work - Niklas B. 2012-04-03 23:36
Well thank you - Jwosty 2012-04-03 23:39