hi,
consider a script foo.rb that contains the line
require ‘./lib/lib.rb’
lib.rb contains the line:
require ‘./lib/annother.rb’
it works.
(require ‘annother.rb’ doesn’t work allthough the file annother.rb is placed in the same directory as lib.rb)
but i am not very pleased with it. the library in the directory lib, must not know from where it is required.
what can i do so that lib can require all it’s files no matter where the script that requires lib.rb is located?
One of the things I most like in Ruby, is that even core functionality can be
easily modified at run-time:
[start messy untested code]
alias :_oldrequire :require
def require(req)
if req=~///
splited=req.split(‘/’)
req_file=splited.pop
req_path=splited.join(‘/’)
oldpath=Dir.pwd
Dir.chdir req_path
ret=_oldrequire(req_file)
Dir.chdir oldpath
return ret
else
_oldrequire(req)
end
end
[end messy untested code]
···
On Tuesday 09 September 2003 16:20, Recheis Meinrad wrote:
hi,
but i am not very pleased with it. the library in the directory lib, must
not know from where it is required. what can i do so that lib can require
all it’s files no matter where the script that requires lib.rb is located?
I think the best thing you can do is put some effort into your code
organisation so that all “requires” assume a common base. This is not
very easy as Ruby has no enforced standards, but it’s not too
difficult once you know how. See http://www.rubygarden.org/ruby?ProjectDirectoryStructure
for some ideas. I should update that soon as I have learnt a lot
more.
To learn the same, play with install.rb from the RAA (project name
“setup” IIRC) and see how another project does it.
Basically, your “requires” should look like this, if your project name
is “foo”:
require “foo/one”
require “foo/another”
To achieve this, your project will be installed at <site_ruby>/1.8/foo
and your development directory structure will look like this:
foo/lib/foo/one.rb
foo/lib/foo/another.rb
There are remaining issues, but that’s enough for now; you may not
even want to separate development and installation at the moment.
It is abnormal to (a) use filesystem paths and (b) explicitly state
the “rb” extension, so I guess you have a lot to learn…
Cheers,
Gavin
···
On Tuesday, September 9, 2003, 11:20:27 PM, Recheis wrote:
hi,
consider a script foo.rb that contains the line
require ‘./lib/lib.rb’
lib.rb contains the line:
require ‘./lib/annother.rb’
it works.
(require ‘annother.rb’ doesn’t work allthough the file
annother.rb is placed in the same directory as lib.rb)
but i am not very pleased with it. the library in the directory lib,
must not know from where it is required. what can i do so that lib
can require all it’s files no matter where the script that requires
lib.rb is located?