我的问题类似于“Ruby中include和extend的区别是什么?”
Ruby中require和include的区别是什么?如果我只是想在我的类中使用模块中的方法,我应该要求它还是包含它?
我的问题类似于“Ruby中include和extend的区别是什么?”
Ruby中require和include的区别是什么?如果我只是想在我的类中使用模块中的方法,我应该要求它还是包含它?
当前回答
What's the difference between "include" and "require" in Ruby? Answer: The include and require methods do very different things. The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method. The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
源
所以如果你只是想使用一个模块,而不是扩展它或做一个混合,那么你会想要使用require。
奇怪的是,Ruby的require类似于C的include,而Ruby的include几乎与C的include完全不同。
其他回答
What's the difference between "include" and "require" in Ruby? Answer: The include and require methods do very different things. The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method. The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
源
所以如果你只是想使用一个模块,而不是扩展它或做一个混合,那么你会想要使用require。
奇怪的是,Ruby的require类似于C的include,而Ruby的include几乎与C的include完全不同。
Ruby的require更像其他语言(比如C语言)中的“include”。它告诉Ruby你想引入另一个文件的内容。在其他语言中类似的机制有: 在c#中使用<namespace>指令。 在Java中导入<package>。 Ruby include是用于mixin的面向对象继承机制。
这里有一个很好的解释:
简单的答案是require和include本质上是不相关的。 “require”类似于C中的include,这可能会让新手感到困惑。 (一个显著的区别是所需文件中的局部变量“蒸发”了 当require完成时。) Ruby包含与C包含完全不同。include语句将一个模块“混合”到一个类中。这是有限形式 多重继承。一个被包含的模块字面上赋予了一个“is-a” 对事物的关系包括它。
重点补充道。
在Ruby元编程书中,
require()方法非常类似于load(),但它的目的是 不同的目的。您使用load()来执行代码,并且使用 Require()导入库。
require(name)
它将返回bolean true/false
作为参数传递给require的名称,ruby将尝试在加载路径中找到具有该名称的源文件。 如果你尝试在第一次之后加载相同的库,require方法将返回' false '。require方法仅在您正在加载的库定义在单独的文件中时才需要使用。 所以它会跟踪那个库是否已经被加载。
include module_name
假设你需要在两个不同的类中有一些方法。这样就不需要在两个类中都写了。相反,你可以在module中定义它。然后将此模块包含在其他类中。 它是由Ruby提供的,只是为了保证DRY原则。它用于DRY代码以避免重复
例如:当你使用require 'math'时,你必须写math::PI。 但是当你使用include math时,你可以简单地写成PI。