要求:无法加载此类文件 -- one_plus (LoadError)

require: cannot load such file -- one_plus (LoadError)

我正在从这个 2008 年的旧教程中学习 rspec 的自定义匹配器 - http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/

项目结构

.
├── one_plus.rb
└── one_plus_spec.rb

我遵循了所有说明,但我无法理解为什么会出现以下错误:

rspec one_plus_spec.rb
/home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- one_plus (LoadError)
    from /home/john/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/john/Code/Rspec/Misc/CustomRspecMatchers/one_plus_spec.rb:3:in `<top (required)>'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$

其他栈溢出问题对此没有明确的答案。我不只是想要一个解决方案,我还想知道为什么会出现这个错误,以及我需要学习哪些概念来防止它再次发生。

尝试使用 require_relative 代替:

require_relative 'one_plus'

./(当前目录)已从 Ruby 1.9 的加载路径中删除。

您的加载路径默认不包含 ./,这就是原因。

Understanding Ruby's load paths 包括详细信息和解决方案。

我个人在规范中经常使用 require_relative,但您也可以修改加载路径。