Understanding Ruby's load paths

前端 未结 3 1860
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 03:39

I\'m a little confused about why my project can\'t load the files it needs, it\'s a really simple project tree:

processor/
  bin/
  lib/
    processor.rb
            


        
相关标签:
3条回答
  • 2020-11-28 04:08

    Use Ruby's require_relative in this case:

    require_relative "processor/mapper"
    
    0 讨论(0)
  • 2020-11-28 04:17

    Ruby's $LOAD_PATH will not include your lib directory by default (even though that's where the file you're running is located).

    You can either tell the ruby interpreter to include it:

    ruby -Ilib lib/processor.rb
    

    Or you can add the lib folder to the load path:

    $LOAD_PATH.unshift(File.dirname(__FILE__))
    require  'processor/mapper'
    ...
    
    0 讨论(0)
  • 2020-11-28 04:26

    Ruby 1.9 no longer includes "." in the load path. Do require "#{File.dirname(__FILE__)}/processor/mapper" or use require_relative.

    You can always determine the current load path by inspecting $LOAD_PATH (or $:)

    (edited: $LOAD_PATH not $:LOAD_PATH)

    0 讨论(0)
提交回复
热议问题