Load only what classes are being used in Ruby?

2019-07-21 09:31发布

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.

2条回答
做自己的国王
2楼-- · 2019-07-21 10:05

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}!"
  end
end
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.

查看更多
爷、活的狠高调
3楼-- · 2019-07-21 10:18

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

查看更多
登录 后发表回答