我的问题类似于“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完全不同。
其他回答
包括 如下所示,当你将一个模块包含到你的类中时,就好像你将模块中定义的代码插入到类中,在那里你“包含”了它。它允许' mixin '行为。它用于DRY代码以避免重复,例如,如果模块中有多个类需要相同的代码。
负载 load方法几乎类似于require方法,除了它不跟踪库是否已加载。因此,可以多次加载一个库,而且在使用load方法时,必须指定“。Rb:库文件名的扩展名。
需要 require方法允许您加载一个库,并防止它被多次加载。如果你尝试在第一次之后加载相同的库,require方法将返回' false '。require方法只需要在你正在加载的库定义在一个单独的文件中,这是通常的情况。
你可以选择这个 http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
下面是require和include之间的一些基本区别:
要求:
Require从文件系统读取文件,解析它,保存到内存中,并在给定的位置运行它,这意味着如果你在脚本运行时更改了任何内容,那么这些更改将不会反映出来。 我们按名称要求文件,而不是按模块名。 它通常用于库和扩展。
包括:
当你将一个模块包含到你的类中时,它的行为就像你将模块中定义的代码插入到你的类中一样。 我们包含模块名,而不是文件名。 它通常用于干涸代码和删除代码中的重复。
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完全不同。
如果你正在使用一个模块,这意味着你要把所有的方法都带入你的类中。 如果你用一个模块扩展一个类,这意味着你把模块的方法作为类方法“引入”了。 如果你在模块中包含了一个类,这意味着你将模块的方法作为实例方法“引入”了。
EX:
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say =>未定义的方法'说'的B:类
B.new.say 这是模块A
C.say 这是模块A
C.new.say =>未定义的方法'说' C:类
您曾经尝试过要求一个模块吗?结果如何?试试:
MyModule = Module.new
require MyModule # see what happens
模块不是必需的,只是包含的!