简体   繁体   English

在Ruby问题中包含文件

[英]Including Files in Ruby Questions

  • I am very new to Ruby so could you please suggest the best practice to separating files and including them. 我是Ruby的新手,所以请你建议分离文件并包含它们的最佳实践。
  • What is the preferred design structure of the file layout. 文件布局的首选设计结构是什么。 When do you decide to separate the algorithm into a new file? 您何时决定将算法分成新文件?
  • When do you use load to include other files and when do you use require ? 什么时候使用load来包含其他文件?什么时候使用require
  • And is there a performance hit when you include files? 当你包含文件时是否有性能损失?

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. loadrequire之间的区别是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. loadrequire文件时,解释器必须解释该文件。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM