[英]Including Files in Ruby Questions
Thanks. 谢谢。
I make one file per class, except classes that are small helper classes, not needed by other files. 我为每个类创建一个文件,除了小辅助类的类,其他文件不需要。 I separate my different modules in subdirectories also. 我也将子目录中的不同模块分开。
The difference between load
and require
is require
will only load the file once, even if it's called multiple times, while load
will load it again regardless of whether it's been loaded before. load
和require
之间的区别是require
只会加载一次文件,即使它被多次调用,而load
也会加载它,无论之前是否加载过。 You'll almost always want to use require
, except maybe in irb
when you want to manually want to reload a file. 您几乎总是想要使用require
,除非您想手动想要重新加载文件,否则可以使用irb
。
I'm not sure on the performance hit. 我不确定性能如何。 When you load
or require
a file, the interpreter has to interpret the file. load
或require
文件时,解释器必须解释该文件。 Most Ruby's will compile it to virtual machine code after being required. 大多数Ruby会在需要后将其编译为虚拟机代码。 Obviously, require
is more performant when the file may have already been included once, because it may not have to load it again. 显然,当文件可能已被包含一次时, require
更具性能,因为它可能不必再次加载它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.