Ruby,包括当前目录中的模块

时间:2022-05-04 04:09:38

I am currently working through the Well Grounded Rubyist. Great book so far. I am stuck on something I don't quite get with ruby. I have two files

我目前正在通过Well Grounded Rubyist工作。到目前为止的好书。我被困在一些我不太喜欢红宝石的东西上。我有两个文件

In ModuleTester.rb

class ModuleTester
  include MyFirstModule
end

mt = ModuleTester.new
mt.say_hello

In MyFirstModule.rb

module MyFirstModule
  def say_hello
    puts "hello"
  end
end

When I run 'ruby ModuleTester.rb', I get the following message:

当我运行'ruby ModuleTester.rb'时,我收到以下消息:

ModuleTester.rb:2:in <class:ModuleTester>': uninitialized constant ModuleTester::MyFirstModule (NameError) from ModuleTester.rb:1:in'

ModuleTester.rb:2:in ':来自ModuleTester.rb的未初始化的常量ModuleTester :: MyFirstModule(NameError):1:in'

From what I have found online, the current directory isn't in the the namespace, so it can't see the file. But, the include statement doesn't take a string to let me give the path. Since the include statement and require statements do different things, I am absolutely lost as to how to get the include statement to recognize the module. I looked through other questions, but they all seem to be using the require statement. Any hints are greatly appreciated.

根据我在网上找到的,当前目录不在命名空间中,因此无法查看该文件。但是,include语句不会使用字符串让我给出路径。由于include语句和require语句做了不同的事情,我绝对不知道如何让include语句识别模块。我查看了其他问题,但他们似乎都在使用require语句。任何提示都非常感谢。

2 个解决方案

#1


8  

You use require to load a file, not include. :-)

您使用require来加载文件,而不是包含。 :-)

First, you have to require the file containing the module you want to include. Then you can include the module.

首先,您必须要求包含要包含的模块的文件。然后您可以包含该模块。

If you're using Rails, it has some magic to automagically require the right file first. But otherwise, you have to do it yourself.

如果您正在使用Rails,那么首先自动要求正确的文件有一些魔力。但除此之外,你必须自己做。

#2


1  

You need to require the file before you can use types defined in it. *

您需要先使用该文件,然后才能使用其中定义的类型。 *

# my_first_module.rb
module MyFirstModule
  def say_hello
    puts 'hello'
  end
end

Note the require at the beginning of the following:

请注意以下开头的要求:

# module_tester.rb
require 'my_first_module'

class ModuleTester
  include MyFirstModule
end

mt = ModuleTester.new
mt.say_hello

The require method actually loads and executes the script specified, using the Ruby VM's load path ($: or $LOAD_PATH) to find it when the argument is not an absolute path.

require方法实际上加载并执行指定的脚本,使用Ruby VM的加载路径($:或$ LOAD_PATH)在参数不是绝对路径时找到它。

The include method, on the other hand actually mixes in a Module's methods into the current class. It's closely related to extend. The Well Grounded Rubyist does a great job of covering all this, though, so I encourage you to continue plugging through it.

另一方面,include方法实际上将Module的方法混合到当前类中。它与扩展密切相关。尽管如此,Well Ground的Rubyist可以很好地覆盖所有这些,所以我鼓励你继续插入它。

See the #require, #include and #extend docs for more information.

有关更多信息,请参阅#require,#include和#extend文档。


* Things work a bit differently when using Rubygems and/or Bundler, but getting into those details is likely to confuse you more than it's worth at this point.

*使用Rubygems和/或Bundler时,情况有所不同,但是进入这些细节可能会让你感到困惑,而不是在这一点上。

#1


8  

You use require to load a file, not include. :-)

您使用require来加载文件,而不是包含。 :-)

First, you have to require the file containing the module you want to include. Then you can include the module.

首先,您必须要求包含要包含的模块的文件。然后您可以包含该模块。

If you're using Rails, it has some magic to automagically require the right file first. But otherwise, you have to do it yourself.

如果您正在使用Rails,那么首先自动要求正确的文件有一些魔力。但除此之外,你必须自己做。

#2


1  

You need to require the file before you can use types defined in it. *

您需要先使用该文件,然后才能使用其中定义的类型。 *

# my_first_module.rb
module MyFirstModule
  def say_hello
    puts 'hello'
  end
end

Note the require at the beginning of the following:

请注意以下开头的要求:

# module_tester.rb
require 'my_first_module'

class ModuleTester
  include MyFirstModule
end

mt = ModuleTester.new
mt.say_hello

The require method actually loads and executes the script specified, using the Ruby VM's load path ($: or $LOAD_PATH) to find it when the argument is not an absolute path.

require方法实际上加载并执行指定的脚本,使用Ruby VM的加载路径($:或$ LOAD_PATH)在参数不是绝对路径时找到它。

The include method, on the other hand actually mixes in a Module's methods into the current class. It's closely related to extend. The Well Grounded Rubyist does a great job of covering all this, though, so I encourage you to continue plugging through it.

另一方面,include方法实际上将Module的方法混合到当前类中。它与扩展密切相关。尽管如此,Well Ground的Rubyist可以很好地覆盖所有这些,所以我鼓励你继续插入它。

See the #require, #include and #extend docs for more information.

有关更多信息,请参阅#require,#include和#extend文档。


* Things work a bit differently when using Rubygems and/or Bundler, but getting into those details is likely to confuse you more than it's worth at this point.

*使用Rubygems和/或Bundler时,情况有所不同,但是进入这些细节可能会让你感到困惑,而不是在这一点上。