Load only what classes are being used in Ruby?

≡放荡痞女 提交于 2019-12-11 03:23:14

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/10002405/load-only-what-classes-are-being-used-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!