开发者

How do I require a file from inside a directory with Ruby?

开发者 https://www.devze.com 2022-12-08 18:53 出处:网络
I think I\'m missing something here.I have a directory like this: myapp |-lib |-package1 |-dostuff.rb |-package2

I think I'm missing something here. I have a directory like this:

myapp
|-lib
  |-package1
    |-dostuff.rb
  |-package2
    |-dostuff.rb

From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:

require 'lib/package1/dostuff'

returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.

What am I forgetting? The two filenames don't have to be the same but that's how they a开发者_如何学编程re to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)


If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.


In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

0

精彩评论

暂无评论...
验证码 换一张
取 消