Laurence Laurence - 1 month ago 10
Ruby Question

Rspec a file which requires others. `require': cannot load such file

Is it possible to run an rspec on a file which requires other files?

my .rb file has the following lines:

require "colorize"
require "./board_initializer"
require "./pieces"


and when running a rake I get the following error:

.rvm/rubies/ruby-2.2.5/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./board_initializer (LoadError)


Thanks!

Answer

The error means board_initializer.rb isn't in the current working directory for __FILE__. Some ways to resolve this include:

  • Providing a valid filename argument to require.
  • Using require_relative with a valid relative path.
  • Modifying the current LOAD_PATH.

There are certainly other ways to resolve this, but they all amount to ensuring that board_initializer.rb can be found by the interpreter when you load or require the file.