Ruby“需要”错误:无法加载此类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9750610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:54:28  来源:igfitidea点击:

Ruby 'require' error: cannot load such file

rubyrequire

提问by The Coding Monk

I've one file, main.rb with the following content:

我有一个文件 main.rb ,内容如下:

require "tokenizer.rb"

The tokenizer.rb file is in the same directoryand its content is:

tokenizer.rb 文件在同一目录下,其内容为:

class Tokenizer
    def self.tokenize(string)
        return string.split(" ")
    end
end

If i try to run main.rb I get the following error:

如果我尝试运行 main.rb,我会收到以下错误:

C:\Documents and Settings\my\src\folder>ruby main.rb

C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- tokenizer.rb (LoadError)
        from C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require '
        from main.rb:1:in `<main>'

I just noticed that if I use loadinstead of requireeverything works fine. What may the problem be here?

我只是注意到,如果我使用load而不是require一切正常。这里可能有什么问题?

回答by Pascal

I just tried and it works with require "./tokenizer". Hope this helps.

我刚刚尝试过,它适用于require "./tokenizer". 希望这可以帮助。

回答by David Grayson

Just do this:

只需这样做:

require_relative 'tokenizer'

If you put this in a Ruby file that is in the same directory as tokenizer.rb, it will work fine no matter what your current working directory (CWD) is.

如果您将它放在与 相同目录中的 Ruby 文件中tokenizer.rb,那么无论您当前的工作目录 (CWD) 是什么,它都可以正常工作。

Explanation of why this is the best way

解释为什么这是最好的方法

The other answers claim you should use require './tokenizer', but that is the wronganswer, because it will only work if you run your Ruby process in the same directory that tokenizer.rbis in. Pretty much the only reason to consider using requirelike that would be if you need to support Ruby 1.8, which doesn't have require_relative.

其他答案声称您应该使用require './tokenizer',但这是错误的答案,因为它仅在您在同一目录中运行 Ruby 进程tokenizer.rb时才有效。考虑使用require类似方法的唯一原因几乎是您需要时支持 Ruby 1.8,它没有require_relative.

The require './tokenizer'answer might work for you today, but it unnecessarily limits the ways in which you can run your Ruby code. Tomorrow, if you want to move your files to a different directory, or just want to start your Ruby process from a different directory, you'll have to rethink all of those requirestatements.

require './tokenizer'今天的答案可能对您有用,但它不必要地限制了您运行 Ruby 代码的方式。明天,如果您想将文件移动到不同的目录,或者只想从不同的目录启动 Ruby 进程,则必须重新考虑所有这些require语句。

Using requireto access files that are on the load path is a fine thing and Ruby gems do it all the time. But you shouldn't start the argument to requirewith a .unless you are doing something very special and know what you are doing.

使用require的,属于负载路径上访问文件是一个很好的事情,红宝石宝石做这一切的时候。但是您不应该require以 a开头,.除非您正在做一些非常特别的事情并且知道自己在做什么。

When you write code that makes assumptions about its environment, you should think carefully about what assumptions to make. In this case, there are up to three different ways to require the tokenizerfile, and each makes a different assumption:

当您编写对其环境做出假设的代码时,您应该仔细考虑要做出哪些假设。在这种情况下,有多达三种不同的方式来要求tokenizer文件,每种方式都做出不同的假设:

  1. require_relative 'path/to/tokenizer': Assumes that the relative path between the two Ruby source files will stay the same.
  2. require 'path/to/tokenizer': Assumes that path/to/tokenizeris inside one of the directories on the load path ($LOAD_PATH). This generally requires extra setup, since you have to add something to the load path.
  3. require './path/to/tokenizer': Assumes that the relative path from the Ruby process's current working directory to tokenizer.rbis going to stay the same.
  1. require_relative 'path/to/tokenizer': 假设两个 Ruby 源文件之间的相对路径将保持不变。
  2. require 'path/to/tokenizer': 假设它path/to/tokenizer位于加载路径 ( $LOAD_PATH)上的目录之一内。这通常需要额外的设置,因为您必须向加载路径添加一些内容。
  3. require './path/to/tokenizer': 假设从 Ruby 进程的当前工作目录到的相对路径tokenizer.rb将保持不变。

I think that for most people and most situations, the assumptions made in options #1 and #2 are more likely to hold true over time.

我认为对于大多数人和大多数情况,选项 #1 和 #2 中的假设随着时间的推移更有可能成立。

回答by Ryan Bigg

Ruby 1.9 has removed the current directory from the load path, and so you will need to do a relative require on this file, as David Grayson says:

Ruby 1.9 已经从加载路径中删除了当前目录,因此您需要对这个文件进行相对要求,正如 David Grayson 所说:

require_relative 'tokenizer'

There's no need to suffix it with .rb, as Ruby's smart enough to know that's what you mean anyway.

没有必要给它加上后缀.rb,因为 Ruby 足够聪明,知道这就是你的意思。

回答by J?rg W Mittag

requireloads a file from the $LOAD_PATH. If you want to require a file relative to the currently executing file instead of from the $LOAD_PATH, use require_relative.

require$LOAD_PATH. 如果您想要一个相对于当前正在执行的文件而不是来自 的文件$LOAD_PATH,请使用require_relative.

回答by Matthew Brock Carey

I would recommend,

我会推荐,

load './tokenizer.rb'

Given, that you know the file is in the same working directory.

鉴于,您知道该文件位于同一工作目录中。

If you're trying to require it relative to the file, you can use

如果您尝试相对于文件要求它,则可以使用

require_relative 'tokenizer'

I hope this helps.

我希望这有帮助。

回答by Pseudomonkey

Another nice little method is to include the current directory in your load path with

另一个不错的小方法是将当前目录包含在加载路径中

$:.unshift('.')

You could push it onto the $: ($LOAD_PATH) array but unshift will force it to load your current working directory before the rest of the load path.

您可以将它推送到 $: ($LOAD_PATH) 数组,但 unshift 将强制它在加载路径的其余部分之前加载您当前的工作目录。

Once you've added your current directory in your load path you don't need to keep specifying

在加载路径中添加当前目录后,您无需继续指定

 require './tokenizer' 

and can just go back to using

并且可以回去使用

require 'tokenizer'

回答by Douglas G. Allen

This will work nicely if it is in a gem lib directory and this is the tokenizer.rb

如果它在 gem lib 目录中并且这是 tokenizer.rb,这将很好地工作

require_relative 'tokenizer/main'

回答by Ananth Srinivasan

I used jruby-1.7.4 to compile my ruby code.

我使用 jruby-1.7.4 来编译我的 ruby​​ 代码。

require 'roman-numerals.rb' 

is the code which threw the below error.

是引发以下错误的代码。

LoadError: no such file to load -- roman-numerals
  require at org/jruby/RubyKernel.java:1054
  require at /Users/amanoharan/.rvm/rubies/jruby-1.7.4/lib/ruby/shared/rubygems/custom_require.rb:36
   (root) at /Users/amanoharan/Documents/Aptana Studio 3 Workspace/RubyApplication/RubyApplication1/Ruby2.rb:2

I removed rb from require and gave

我从 require 中删除了 rb 并给出了

require 'roman-numerals' 

It worked fine.

它工作得很好。

回答by Hans-J. Schmid

What about including the current directory in the search path?

将当前目录包含在搜索路径中怎么样?

ruby -I. main.rb

回答by Andy Barnett

For those who are absolutely sure their relative path is correct, my problem was that my files did not have the .rbextension! (Even though I used RubyMineto create the files and selected that they were Rubyfiles on creation.)

对于那些绝对确定其相对路径正确的人,我的问题是我的文件没有.rb扩展名!(即使我曾经RubyMine创建文件并选择它们是创建时的文件Ruby。)

Double check the file extensions on your file!

仔细检查文件上的文件扩展名!