My question is similar to "What is the difference between include and extend in Ruby?".
我的问题类似于“在Ruby中包含和扩展的区别是什么?”
What's the difference between require
and include
in Ruby? If I just want to use the methods from a module in my class, should I require
it or include
it?
Ruby中的require和include有什么区别?如果我只是想使用类中的模块中的方法,我应该要求它还是应该包含它?
10 个解决方案
#1
495
What's the difference between "include" and "require" in Ruby?
Ruby中的“include”和“require”有什么区别?
Answer:
答:
The include and require methods do very different things.
include和require方法可以做非常不同的事情。
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.
require方法完成了大多数其他编程语言中的include操作:运行另一个文件。它还可以跟踪您过去需要的内容,并且不会两次需要相同的文件。要运行另一个没有添加此功能的文件,可以使用load方法。
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.
include方法从另一个模块获取所有方法,并将它们包含到当前模块中。这是一种语言级别的东西,而不是文件级别的东西。include方法是使用其他模块“扩展”类的主要方法(通常称为mix-in)。例如,如果您的类定义了方法“each”,您可以包含mixin模块Enumerable,它可以作为集合。这可能会让人感到困惑,因为include谓词在其他语言中的用法非常不同。
源
So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require
.
因此,如果你只想使用一个模块,而不是扩展它或做一个混合,那么你将需要使用需求。
Oddly enough, Ruby's require
is analogous to C's include
, while Ruby's include
is almost nothing like C's include
.
奇怪的是,Ruby的需求类似于C的include,而Ruby的include几乎与C的include完全不同。
#2
86
From the Metaprogramming Ruby book,
从元编程Ruby书籍中,
The
require()
method is quite similar toload()
, but it’s meant for a different purpose. You useload()
to execute code, and you userequire()
to import libraries.require()方法与load()非常相似,但用途不同。使用load()执行代码,使用require()导入库。
#3
77
If you're using a module, that means you're bringing all the methods into your class. If you extend
a class with a module, that means you're "bringing in" the module's methods as class methods. If you include
a class with a module, that means you're "bringing in" the module's methods as instance methods.
如果您正在使用一个模块,这意味着您将把所有的方法都引入到您的类中。如果你用一个模块扩展一个类,那就意味着你将模块的方法作为类方法“引入”。如果您包含一个带有模块的类,这意味着您将“引入”模块的方法作为实例方法。
EX:
例:
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say
=> undefined method 'say' for B:Class
B。say => undefined method‘say’for B:Class
B.new.say
=> this is module A
B.new。这是A模块。
C.say
=> this is module A
C。这是A模块。
C.new.say
=> undefined method 'say' for C:Class
C.new。对于C:Class, say =>未定义的方法‘say’
#4
53
-
Ruby
require
is more like "include" in other languages (such as C). It tells Ruby that you want to bring in the contents of another file. Similar mechanisms in other languages are:Ruby require更类似于其他语言(如C)中的“include”,它告诉Ruby您希望引入另一个文件的内容。其他语文的类似机制是:
-
using <namespace>
directive in C#. -
在c#中使用
指令。 -
import <package>
in Java. - 在Java中导入 <包> 。
-
-
Ruby
include
is an object-oriented inheritance mechanism used for mixins.Ruby includeis是一种用于mixin的面向对象的继承机制。
There is a good explanation here:
这里有一个很好的解释:
[The] simple answer is that require and include are essentially unrelated.
简单的答案是,需要和包含本质上是不相关的。
"require" is similar to the C include, which may cause newbie confusion. (One notable difference is that locals inside the required file "evaporate" when the require is done.)
“require”类似于C include,这可能会导致新手混淆。(一个显著的区别是,当完成需求时,所需文件“蒸发”中的本地变量。)
The Ruby include is nothing like the C include. The include statement "mixes in" a module into a class. It's a limited form of multiple inheritance. An included module literally bestows an "is-a" relationship on the thing including it.
Ruby include与C include完全不同。include语句“混合”一个模块到一个类中。它是一种有限形式的多重继承。包含的模块实际上赋予了包含它的东西一个“is-a”关系。
Emphasis added.
重点补充道。
#5
7
Have you ever tried to require
a module? What were the results? Just try:
您曾经尝试过需要一个模块吗?结果是什么?试试:
MyModule = Module.new
require MyModule # see what happens
Modules cannot be required, only included!
模块不能被要求,只包括在内!
#6
6
From Programming Ruby 1.9
从编程Ruby 1.9
We’ll make a couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a module. If that module is in a separate file, you must use require (or its less commonly used cousin, load) to drag that file in before using include. Second, a Ruby include does not simply copy the module’s instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they’ll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.
在继续之前,我们将对include语句做一些说明。首先,它与文件无关。C程序员使用一个叫做#include的预处理指令在编译期间将一个文件的内容插入到另一个文件中。Ruby include语句只是对模块的引用。如果该模块位于一个单独的文件中,那么在使用include之前,您必须使用require(或其不常用的表亲load)将该文件拖到其中。其次,Ruby include并不简单地将模块的实例方法复制到类中。相反,它从类中引用所包含的模块。如果多个类包括那个模块,它们都指向相同的东西。如果在模块中更改方法的定义,即使在程序运行时,包含该模块的所有类都将显示新的行为。
#7
3
require(name)
It will return bolean true/false
它将返回真/假。
The name which is passed as parameter to the require, ruby will try to find the source file with that name in your load path. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file. So it keeps track of whether that library was already loaded or not.
作为参数传递给require的名称,ruby将尝试在您的加载路径中查找具有该名称的源文件。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。因此它会跟踪那个库是否已经被加载。
include module_name
Suppose if you have some methods that you need to have in two different classes. Then you don't have to write them in both the classes. Instead what you can do is, define it in module. And then include this module in other classes. It is provided by Ruby just to ensure DRY principle. It’s used to DRY up your code to avoid duplication
假设您在两个不同的类中有一些方法。这样你就不必在两个类中都写了。相反,您可以做的是,在模块中定义它。然后在其他类中包含这个模块。它由Ruby提供,以确保干燥的原理。它用于使代码干燥以避免重复
#8
2
Include When you Include a module into your class as shown below, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.
当您将一个模块包含到您的类中时(如下所示),就好像您将模块中定义的代码插入到类中,在类中“包含”它。它允许“混合”行为。它用于使代码干燥以避免重复,例如,如果模块中有多个类需要相同的代码。
Load The load method is almost like the require method except it doesn’t keep track of whether or not that library has been loaded. So it’s possible to load a library multiple times and also when using the load method you must specify the “.rb” extension of the library file name.
Load方法几乎与require方法相似,只是它没有跟踪库是否已加载。因此,可以多次加载一个库,而且在使用load方法时,必须指定“”。程序库文件名的扩展名。
Require The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.
要求方法允许您加载一个库并防止它被加载不止一次。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。
You can prefer this http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
您可以选择http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
#9
0
Include
包括
When you
include
a module into your class, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.当您将一个模块包含到类中时,就好像您将模块中定义的代码插入到类中,在类中“包含”它。它允许“混合”行为。它用于使代码干燥以避免重复,例如,如果模块中有多个类需要相同的代码。
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
include Log
# ...
end
tc = TestClass.new.class_type # -> success
tc = TestClass.class_type # -> error
Require
需要
The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.
require方法允许您加载一个库,并防止它多次加载。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。
So it keeps track of whether that library was already loaded or not. You also don’t need to specify the “.rb” extension of the library file name. Here’s an example of how to use require. Place the require method at the very top of your “.rb” file:
因此,它会跟踪是否已经加载了库。您也不需要指定“”。程序库文件名的扩展名。这里有一个如何使用require的例子。把require方法放在最上面。rb”文件:
Load
负载
The load method is almost like the require method except it doesn’t keep track of whether or not that library has been loaded. So it’s possible to load a library multiple times and also when using the load method you must specify the “.rb” extension of the library file name.
load方法几乎与require方法相似,只是它没有跟踪库是否已加载。因此,可以多次加载一个库,而且在使用load方法时,必须指定“”。程序库文件名的扩展名。
Extend
扩展
When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods.
当使用扩展方法而不是include时,您是将模块的方法添加为类方法而不是实例方法。
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
extend Log
# ...
end
tc = TestClass.class_type
#10
0
Below are few basic differences between require and include:
以下是要求和包含的基本区别:
Require:
要求:
- Require reads the file from the file system, parses it, saves to the memory and runs it in a given place which means if you will even change anything while the script is running than that change will not reflect.
- 需要从文件系统中读取文件,解析它,保存到内存中,并在给定的位置运行它,这意味着如果您在脚本运行时更改任何内容,那么该更改将不会反映出来。
- We require file by name, not by module name.
- 我们要求文件名,而不是模块名。
- It is typically used for libraries and extensions.
- 它通常用于库和扩展。
Include:
包括:
- When you include a module into your class it behaves as if you took the code defined in your module and inserted it in your class.
- 当您将一个模块包含到类中时,它的行为就好像您将模块中定义的代码插入到类中一样。
- We include module name, not the file name.
- 我们包括模块名,而不是文件名。
- It is typically used to dry up the code and to remove duplication in the code.
- 它通常用于干枯代码并删除代码中的重复。
#1
495
What's the difference between "include" and "require" in Ruby?
Ruby中的“include”和“require”有什么区别?
Answer:
答:
The include and require methods do very different things.
include和require方法可以做非常不同的事情。
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.
require方法完成了大多数其他编程语言中的include操作:运行另一个文件。它还可以跟踪您过去需要的内容,并且不会两次需要相同的文件。要运行另一个没有添加此功能的文件,可以使用load方法。
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.
include方法从另一个模块获取所有方法,并将它们包含到当前模块中。这是一种语言级别的东西,而不是文件级别的东西。include方法是使用其他模块“扩展”类的主要方法(通常称为mix-in)。例如,如果您的类定义了方法“each”,您可以包含mixin模块Enumerable,它可以作为集合。这可能会让人感到困惑,因为include谓词在其他语言中的用法非常不同。
源
So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require
.
因此,如果你只想使用一个模块,而不是扩展它或做一个混合,那么你将需要使用需求。
Oddly enough, Ruby's require
is analogous to C's include
, while Ruby's include
is almost nothing like C's include
.
奇怪的是,Ruby的需求类似于C的include,而Ruby的include几乎与C的include完全不同。
#2
86
From the Metaprogramming Ruby book,
从元编程Ruby书籍中,
The
require()
method is quite similar toload()
, but it’s meant for a different purpose. You useload()
to execute code, and you userequire()
to import libraries.require()方法与load()非常相似,但用途不同。使用load()执行代码,使用require()导入库。
#3
77
If you're using a module, that means you're bringing all the methods into your class. If you extend
a class with a module, that means you're "bringing in" the module's methods as class methods. If you include
a class with a module, that means you're "bringing in" the module's methods as instance methods.
如果您正在使用一个模块,这意味着您将把所有的方法都引入到您的类中。如果你用一个模块扩展一个类,那就意味着你将模块的方法作为类方法“引入”。如果您包含一个带有模块的类,这意味着您将“引入”模块的方法作为实例方法。
EX:
例:
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say
=> undefined method 'say' for B:Class
B。say => undefined method‘say’for B:Class
B.new.say
=> this is module A
B.new。这是A模块。
C.say
=> this is module A
C。这是A模块。
C.new.say
=> undefined method 'say' for C:Class
C.new。对于C:Class, say =>未定义的方法‘say’
#4
53
-
Ruby
require
is more like "include" in other languages (such as C). It tells Ruby that you want to bring in the contents of another file. Similar mechanisms in other languages are:Ruby require更类似于其他语言(如C)中的“include”,它告诉Ruby您希望引入另一个文件的内容。其他语文的类似机制是:
-
using <namespace>
directive in C#. -
在c#中使用
指令。 -
import <package>
in Java. - 在Java中导入 <包> 。
-
-
Ruby
include
is an object-oriented inheritance mechanism used for mixins.Ruby includeis是一种用于mixin的面向对象的继承机制。
There is a good explanation here:
这里有一个很好的解释:
[The] simple answer is that require and include are essentially unrelated.
简单的答案是,需要和包含本质上是不相关的。
"require" is similar to the C include, which may cause newbie confusion. (One notable difference is that locals inside the required file "evaporate" when the require is done.)
“require”类似于C include,这可能会导致新手混淆。(一个显著的区别是,当完成需求时,所需文件“蒸发”中的本地变量。)
The Ruby include is nothing like the C include. The include statement "mixes in" a module into a class. It's a limited form of multiple inheritance. An included module literally bestows an "is-a" relationship on the thing including it.
Ruby include与C include完全不同。include语句“混合”一个模块到一个类中。它是一种有限形式的多重继承。包含的模块实际上赋予了包含它的东西一个“is-a”关系。
Emphasis added.
重点补充道。
#5
7
Have you ever tried to require
a module? What were the results? Just try:
您曾经尝试过需要一个模块吗?结果是什么?试试:
MyModule = Module.new
require MyModule # see what happens
Modules cannot be required, only included!
模块不能被要求,只包括在内!
#6
6
From Programming Ruby 1.9
从编程Ruby 1.9
We’ll make a couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a module. If that module is in a separate file, you must use require (or its less commonly used cousin, load) to drag that file in before using include. Second, a Ruby include does not simply copy the module’s instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they’ll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.
在继续之前,我们将对include语句做一些说明。首先,它与文件无关。C程序员使用一个叫做#include的预处理指令在编译期间将一个文件的内容插入到另一个文件中。Ruby include语句只是对模块的引用。如果该模块位于一个单独的文件中,那么在使用include之前,您必须使用require(或其不常用的表亲load)将该文件拖到其中。其次,Ruby include并不简单地将模块的实例方法复制到类中。相反,它从类中引用所包含的模块。如果多个类包括那个模块,它们都指向相同的东西。如果在模块中更改方法的定义,即使在程序运行时,包含该模块的所有类都将显示新的行为。
#7
3
require(name)
It will return bolean true/false
它将返回真/假。
The name which is passed as parameter to the require, ruby will try to find the source file with that name in your load path. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file. So it keeps track of whether that library was already loaded or not.
作为参数传递给require的名称,ruby将尝试在您的加载路径中查找具有该名称的源文件。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。因此它会跟踪那个库是否已经被加载。
include module_name
Suppose if you have some methods that you need to have in two different classes. Then you don't have to write them in both the classes. Instead what you can do is, define it in module. And then include this module in other classes. It is provided by Ruby just to ensure DRY principle. It’s used to DRY up your code to avoid duplication
假设您在两个不同的类中有一些方法。这样你就不必在两个类中都写了。相反,您可以做的是,在模块中定义它。然后在其他类中包含这个模块。它由Ruby提供,以确保干燥的原理。它用于使代码干燥以避免重复
#8
2
Include When you Include a module into your class as shown below, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.
当您将一个模块包含到您的类中时(如下所示),就好像您将模块中定义的代码插入到类中,在类中“包含”它。它允许“混合”行为。它用于使代码干燥以避免重复,例如,如果模块中有多个类需要相同的代码。
Load The load method is almost like the require method except it doesn’t keep track of whether or not that library has been loaded. So it’s possible to load a library multiple times and also when using the load method you must specify the “.rb” extension of the library file name.
Load方法几乎与require方法相似,只是它没有跟踪库是否已加载。因此,可以多次加载一个库,而且在使用load方法时,必须指定“”。程序库文件名的扩展名。
Require The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.
要求方法允许您加载一个库并防止它被加载不止一次。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。
You can prefer this http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
您可以选择http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
#9
0
Include
包括
When you
include
a module into your class, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.当您将一个模块包含到类中时,就好像您将模块中定义的代码插入到类中,在类中“包含”它。它允许“混合”行为。它用于使代码干燥以避免重复,例如,如果模块中有多个类需要相同的代码。
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
include Log
# ...
end
tc = TestClass.new.class_type # -> success
tc = TestClass.class_type # -> error
Require
需要
The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.
require方法允许您加载一个库,并防止它多次加载。如果在第一次加载相同的库之后,require方法将返回' false '。如果正在加载的库是在单独的文件中定义的,那么只需要使用require方法。
So it keeps track of whether that library was already loaded or not. You also don’t need to specify the “.rb” extension of the library file name. Here’s an example of how to use require. Place the require method at the very top of your “.rb” file:
因此,它会跟踪是否已经加载了库。您也不需要指定“”。程序库文件名的扩展名。这里有一个如何使用require的例子。把require方法放在最上面。rb”文件:
Load
负载
The load method is almost like the require method except it doesn’t keep track of whether or not that library has been loaded. So it’s possible to load a library multiple times and also when using the load method you must specify the “.rb” extension of the library file name.
load方法几乎与require方法相似,只是它没有跟踪库是否已加载。因此,可以多次加载一个库,而且在使用load方法时,必须指定“”。程序库文件名的扩展名。
Extend
扩展
When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods.
当使用扩展方法而不是include时,您是将模块的方法添加为类方法而不是实例方法。
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
extend Log
# ...
end
tc = TestClass.class_type
#10
0
Below are few basic differences between require and include:
以下是要求和包含的基本区别:
Require:
要求:
- Require reads the file from the file system, parses it, saves to the memory and runs it in a given place which means if you will even change anything while the script is running than that change will not reflect.
- 需要从文件系统中读取文件,解析它,保存到内存中,并在给定的位置运行它,这意味着如果您在脚本运行时更改任何内容,那么该更改将不会反映出来。
- We require file by name, not by module name.
- 我们要求文件名,而不是模块名。
- It is typically used for libraries and extensions.
- 它通常用于库和扩展。
Include:
包括:
- When you include a module into your class it behaves as if you took the code defined in your module and inserted it in your class.
- 当您将一个模块包含到类中时,它的行为就好像您将模块中定义的代码插入到类中一样。
- We include module name, not the file name.
- 我们包括模块名,而不是文件名。
- It is typically used to dry up the code and to remove duplication in the code.
- 它通常用于干枯代码并删除代码中的重复。