"require and subdirectories" problem

T

Tobias Peters

def import(what)
require File.join(File.dirname(caller[0]), what)
end

and use "import" instead of "require".
 
R

Recheis Meinrad

This is a multi-part message in MIME format.

------_=_NextPart_001_01C376D5.4988B134
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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.=20
(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?


- henon


------_=_NextPart_001_01C376D5.4988B134
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
6.0.5762.3">
<TITLE>&quot;require and subdirectories&quot; problem</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->

<P><FONT SIZE=3D2 FACE=3D"Arial">hi,</FONT>

<BR><FONT SIZE=3D2 FACE=3D"Arial">consider a script foo.rb that contains =
the line</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">require './lib/lib.rb'</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">lib.rb contains the line:</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">require './lib/annother.rb'</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">it works. </FONT>

<BR><FONT SIZE=3D2 FACE=3D"Arial">(require 'annother.rb'&nbsp; doesn't =
work allthough the file annother.rb is placed in the same directory as =
lib.rb)</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">but i am not very pleased with it. the =
library in the directory lib, must not know from where it is =
required.</FONT>

<BR><FONT SIZE=3D2 FACE=3D"Arial">what can i do so that lib can require =
all it's files no matter where the script that requires lib.rb is =
located?</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Arial">- henon</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C376D5.4988B134--
 
I

Idan Sofer

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?
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top