简体   繁体   English

我不知道红宝石的要求

[英]I can't figure out the require in ruby

I'm new to Ruby 我是Ruby的新手

MakModule.rb MakModule.rb

module Display
  class Multiply
    def Multiply.mul(first, second)
      return first * second
    end
  end
end

MakRequire1.rb MakRequire1.rb

require "Display"
puts Multiply.mul 5,6

MakRequire2.rb MakRequire2.rb

require "MakModule.rb"
puts Multiply.mul 5,6

both file give me the error below 两个文件都给我下面的错误

ruby: No such file or directory -- makRequire (LoadError)

How should I correct my code? 我应该如何更正我的代码?

It is simply impossible that the code you posted here generates that error message. 您在此处发布的代码根本不可能生成该错误消息。 The error message says that you tried to require a file named makRequire , but that filename doesn't appear anywhere in the code you posted. 错误消息表明您试图require一个名为makRequire的文件,但该文件名未出现在您发布的代码中的任何位置。

Without the actual code that is generating the actual error, it is impossible to answer your question. 如果没有生成实际错误的实际代码,就不可能回答您的问题。 However, here are a few general tips: 但是,这里有一些一般性提示:

Whenever the computer tells you that it cannot find something, in 99% of the cases, the problem is that the thing the computer tells it couldn't find isn't actually there. 每当计算机告诉您找不到任何东西时,在99%的情况下,问题是计算机告诉您找不到的东西实际上并不存在。

So, in this case, the computer tells you that it cannot find a file named makRequire.rb , and the most likely explanation for that is that makRequire.rb doesn't actually exist. 因此,在这种情况下,计算机告诉您找不到名为makRequire.rb的文件,而最有可能的解释是makRequire.rb实际上并不存在。 So, the first thing you need to check is: does makRequire.rb (note the capitalization and the file extension) actually exist? 所以,你需要检查的第一件事就是: makRequire.rb (注意大小写和文件扩展名)实际上存在吗? Because if it doesn't exist, then the reason why the computer cannot find it, should be rather obvious. 因为如果它不存在,那么计算机找不到它的原因应该很明显。

In 99% of the rest of the cases, the problem is that the thing the computer is looking for does exist, but the computer is looking in the wrong place. 其余 99%的情况下,问题是计算机正在寻找的东西确实存在,但是计算机在错误的位置。 So, after you have verified that makRequire.rb actually does exist, you need to make sure that the directory the file is in, is in Ruby's $LOAD_PATH , and if it isn't, you need to add that directory to Ruby's $LOAD_PATH . 因此,在确认makRequire.rb实际上确实存在之后,您需要确保文件所在的目录位于Ruby的$LOAD_PATH ,如果不存在,则需要将该目录添加到Ruby的$LOAD_PATH Alternatively, if you want to require the file relative to the path of the file that is doing the requiring, you need to use require_relative instead of require . 或者,如果要相对于执行require的文件路径来require文件,则需要使用require_relative而不是require

The third thing to check for, is whether the user who own the ruby process has sufficient privileges to access the file makRequire.rb , the directory it is in and all of its parent directories. 要检查的第三件事是,拥有ruby进程的用户是否具有足够的特权来访问文件makRequire.rb ,文件makRequire.rb的目录及其所有父目录。

尝试这个,

require File.join(File.dirname(__FILE__),'MarkModule')

Try require './MakModule' , because the . 请尝试require './MakModule' ,因为. is the current directory. 是当前目录。

require 'MakModule'

You can require a file that is in the same directory. 您可以要求文件位于同一目录中。 To use a module you would typically include the module inside a class definition. 要使用模块,通常将模块include在类定义中。 So you would never require Display , you would require the file that contains Display (without the .rb extension, usually). 因此,您永远不需要Display ,而需要包含Display的文件(通常不带.rb扩展名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM