require cannot load such file

a 夏天 提交于 2020-01-06 14:16:23

问题


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>'

回答1:


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"


来源:https://stackoverflow.com/questions/23189522/require-cannot-load-such-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!