Question

I have the following 2 files in the same dir:

churn.rb:

 subsystem_names = ['audit', 'fulfillment', 'persistence',    
                     'ui', 'util', 'inventory']
  start_date = month_before(Time.now)       

  puts header(start_date)                   
  subsystem_names.each do | name |
    puts subsystem_line(name, change_count_for(name))   
  end

churn_test.rb:

require "test/unit"
require "churn"    

class ChurnTests < Test::Unit::TestCase 

  def test_month_before_is_28_days
    assert_equal(Time.local(2005, 1, 1),
                 month_before(Time.local(2005, 1, 29)))
  end

end

When I run churn_test.rb I get the following error:

/Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- churn (LoadError)
    from /Users/ca/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from churn-tests.rb:8:in `<main>'
Was it helpful?

Solution

When you require sth, Ruby searches the source in $LOAD_PATH ($:). For security concern, the current working directory is not included in $LOAD_PATH. You can either explicitly add the directory into $LOAD_PATH

$: << File.dirname(__FILE__)

require 'churn'

or use the Kernel#require_relative to require a module based on the same directory:

require_relative "churn"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top