Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

Understanding Ruby's load paths

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
    processor/
      mapper.rb
      reducer.rb

and my processor.rb file looks like

require 'processor/mapper'
require 'processor/reducer'

class Processor

end

And just for testing it that file mapper looks like:

class Mapper
  def run
    puts "running map"
  end
end

But running ruby lib/processor.rb results in:

<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- processor/mapper (LoadError)
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from lib/processor.rb:3:in `<class:Processor>'
    from lib/processor.rb:2:in `<main>'    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...