在Ruby中使用require的最佳实践是什么?

时间:2023-02-10 20:04:25

Some models require other models to be loaded first. But each required file only needs to be loaded once.

某些型号需要先装载其他型号。但每个必需的文件只需要加载一次。

What is the best way to manage this? Put all the require lines in a file (like init.rb), or require files at the top of each model file?

管理这个的最佳方法是什么?将所有需求行放在一个文件中(如init.rb),或者需要每个模型文件顶部的文件?

2 个解决方案

#1


11  

Let's evaluate each option:

让我们评估每个选项:

  1. Put all the require lines in a file (like init.rb)

    将所有需求行放在一个文件中(如init.rb)

    This means each individual file will be less cluttered, as requires will all be in one place. However, it can happen that the order in which they are written matters, so you end up effectively doing dependency resolution manually in this file.

    这意味着每个单独的文件将不那么混乱,因为所有文件都在一个地方。但是,它们的编写顺序可能会发生,因此您最终会在此文件中手动执行依赖项解析。

  2. require files at the top of each model file

    需要每个模型文件顶部的文件

    Each file will have a little more content, but you won't have to worry about ordering as each file explicitly requires the dependencies it needs. Calling require for the same file multiple times has no effect.

    每个文件都会有更多内容,但您不必担心排序,因为每个文件明确需要它所需的依赖项。多次调用同一文件的require不起作用。

    This also means that you can require only parts of your code, which is useful for libraries; e.g. require active_support/core_ext/date/calculations gets only the part of the library the external app needs.

    这也意味着您只需要部分代码,这对库很有用;例如require active_support / core_ext / date / computation只获取外部应用程序所需的库部分。

Of the two, I'd pick the second. It's cleaner, requires less thinking, and makes your code much more modular.

在这两个中,我会选择第二个。它更干净,需要更少的思考,并使您的代码更加模块化。

#2


2  

For each file, require within that file all the files that it depends on. It does not harm to have duplicates with other files because each file is only required once. That is the purpose of the require method.

对于每个文件,在该文件中需要它依赖的所有文件。与其他文件重复并不会有害,因为每个文件只需要一次。这就是require方法的目的。

#1


11  

Let's evaluate each option:

让我们评估每个选项:

  1. Put all the require lines in a file (like init.rb)

    将所有需求行放在一个文件中(如init.rb)

    This means each individual file will be less cluttered, as requires will all be in one place. However, it can happen that the order in which they are written matters, so you end up effectively doing dependency resolution manually in this file.

    这意味着每个单独的文件将不那么混乱,因为所有文件都在一个地方。但是,它们的编写顺序可能会发生,因此您最终会在此文件中手动执行依赖项解析。

  2. require files at the top of each model file

    需要每个模型文件顶部的文件

    Each file will have a little more content, but you won't have to worry about ordering as each file explicitly requires the dependencies it needs. Calling require for the same file multiple times has no effect.

    每个文件都会有更多内容,但您不必担心排序,因为每个文件明确需要它所需的依赖项。多次调用同一文件的require不起作用。

    This also means that you can require only parts of your code, which is useful for libraries; e.g. require active_support/core_ext/date/calculations gets only the part of the library the external app needs.

    这也意味着您只需要部分代码,这对库很有用;例如require active_support / core_ext / date / computation只获取外部应用程序所需的库部分。

Of the two, I'd pick the second. It's cleaner, requires less thinking, and makes your code much more modular.

在这两个中,我会选择第二个。它更干净,需要更少的思考,并使您的代码更加模块化。

#2


2  

For each file, require within that file all the files that it depends on. It does not harm to have duplicates with other files because each file is only required once. That is the purpose of the require method.

对于每个文件,在该文件中需要它依赖的所有文件。与其他文件重复并不会有害,因为每个文件只需要一次。这就是require方法的目的。