Ruby中include和require有什么区别?

What is the difference between include and require in Ruby?

我的问题类似于"Ruby中include和extend的区别是什么?".

红宝石中的requireinclude有什么区别?如果我只想使用类中某个模块的方法,我应该使用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完全不同。


如果您使用的是一个模块,这意味着您将把所有方法引入到您的类中。如果您使用一个带有模块的类,这意味着您将"引入"模块的方法作为类方法。如果您使用一个带有模块的类include,这意味着您将"引入"模块的方法作为实例方法。

前任:

1
2
3
4
5
6
7
8
9
10
11
12
13
 module A
   def say
     puts"this is module A"
   end
 end

 class B
   include A
 end

 class C
   extend A
 end

B.say=>B:类的未定义方法'say'

B.new.say=>这是模块A

C.say=>这是模块A

C.new.say=>C:类的未定义方法'say'


从元编程Ruby书中,

The require() method is quite similar to load(), but it’s meant for
a different purpose. You use load() to execute code, and you use
require() to import libraries.


  • Rubyrequire在其他语言(如C)中更像"include"。它告诉Ruby您想要引入另一个文件的内容。其他语言中的类似机制是:

    • c中的using 指令。
    • 在Java中,EDCOX1为7。
  • Rubyinclude是一种面向对象的继承机制,用于混合。

这里有一个很好的解释:

[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.)

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.

强调添加。


来自编程Ruby1.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.


你曾经尝试过使用一个模块吗?结果如何?试一试:

1
2
MyModule = Module.new
require MyModule # see what happens

模块不能是必需的,只包括在内!


包括当您将一个模块包含到类中(如下所示)时,就好像您获取了模块中定义的代码并将其插入到类中,然后在其中"包含"它。它允许"混合"行为。例如,如果模块中有多个类需要相同的代码,那么它可以用来擦干代码以避免重复。

负载加载方法与Require方法几乎相同,只是它不跟踪库是否已加载。因此可以多次加载库,并且在使用加载方法时,必须指定库文件名的扩展名".rb"。

要求Require方法允许您加载库,并防止它被多次加载。如果第一次尝试加载同一个库,则Require方法将返回"false"。仅当正在加载的库在单独的文件中定义时才需要使用Require方法,这种情况通常是这样的。

你可以喜欢这个http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/


1
require(name)

它将返回Bolean真/假

作为参数传递给Require的名称,Ruby将尝试在加载路径中查找具有该名称的源文件。如果第一次尝试加载同一个库,则Require方法将返回"false"。仅当正在加载的库在单独的文件中定义时,才需要使用Require方法。所以它跟踪库是否已经加载。

1
include module_name

假设您有一些方法需要在两个不同的类中使用。那你就不用在两个班上都写了。相反,您可以在模块中定义它。然后将这个模块包含在其他类中。它由Ruby提供,只是为了确保干燥。它用来擦干代码以避免重复


以下是需求和包括的基本区别:

要求:

  • require从文件系统中读取文件,解析文件,保存到内存,并在给定的位置运行文件,这意味着即使在脚本运行时更改任何内容,也不会反映该更改。
  • 我们需要按名称,而不是按模块名称。
  • 它通常用于库和扩展。
  • 包括:

  • 当您在类中包含一个模块时,它的行为就好像您获取了模块中定义的代码并将其插入到类中一样。
  • 我们包括模块名,而不是文件名。
  • 它通常用于擦干代码并消除代码中的重复。

  • 包括

    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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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

    要求

    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.

    所以它跟踪库是否已经加载。您也不需要指定库文件名的扩展名".rb"。下面是一个如何使用Require的示例。将Require方法放在".rb"文件的最上面:

    负载

    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.

    延伸

    When using the extend method instead of include, you are adding the
    module’s methods as class methods instead of as instance methods.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    module Log
      def class_type
       "This class is of type: #{self.class}"
      end
    end

    class TestClass
      extend Log
      # ...
    end

    tc = TestClass.class_type

    在回答这个问题之前,我将向您简要介绍为什么在PHP中使用include()和require()。

    在服务器执行之前,将一个PHP文件的内容插入另一个PHP文件。现在,关于include()require()的细节已经很少了。

    include()-包含指定的文件。如果找不到文件并执行其余的脚本,它将生成一个警告。include()应该在不需要该文件时使用,而在找不到该文件时应用程序流应该继续。

    语法-

    include ‘filename’;

    不使用include(),我们可以使用include_once()

    include_once()-它还包括一个指定的文件,但如果已经包含了一个文件。它将不再包括在内。如果找不到文件并执行其余的脚本,也应发出警告。

    语法-

    include_once();

    require()-它还包含一个指定的文件,但是如果找不到该文件并停止执行,它将抛出一个致命错误(e_compile_error)。

    句法

    require ‘filename’;

    我们可以使用require_once();,而不是使用require()

    require_once()-它还包括一个指定的文件,但是如果已经包含了一个文件。它将不再包括在内。如果找不到文件并停止执行,它将抛出一个致命错误(E_COMPILE_ERROR)。

    句法

    require_once();