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

时间:2015-01-03 00:44:52

标签: ruby

我正在从这个旧的2008教程 - http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/

学习rspec的自定义匹配器

项目结构

.
├── 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$

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

2 个答案:

答案 0 :(得分:2)

请尝试使用require_relative

require_relative 'one_plus'

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

答案 1 :(得分:0)

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

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

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