[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

doing require on some relateive path

Junkone

12/14/2007 10:42:00 PM

hello
I have the following 2 files

Someopath/Dir1/File1.rb
Someopath/Dir2/File2.rb

How can i do a require 'relativepath/Dir2/File2.rb' from File1.rb

appreciate any help.
5 Answers

John Pywtorak

12/14/2007 11:16:00 PM

0

Hi Jukone,

The idiom I have seen used and use myself is...
withing File1.rb

require File.dirname(__FILE__) + '/../../Dir1/File1.rb'

Hope that helps.

Johnny P

Junkone wrote:
> hello
> I have the following 2 files
>
> Someopath/Dir1/File1.rb
> Someopath/Dir2/File2.rb
>
> How can i do a require 'relativepath/Dir2/File2.rb' from File1.rb
>
> appreciate any help.
>
>

Kevin Barnes

12/15/2007 1:17:00 PM

0

Some recommend being a bit more implicit with the path structure. So
more along the lines of,

require File.join(File.dirname(__FILE__), '..', '..', 'Dir1',
'File1'))

File.join will apply the proper file separator for any given platform
running your code. Btw, there is no need to put the '.rb' extension -
it is implied.

I have been meaning to finish my 'relative require' gem, http://elreq.rub...,
that allows you to just do the following,

require '../../Dir1/File1'

Which I find much nicer, cleaner and easier. The gem is finished, I
just need to publish it. I'll do that this weekend.

Kevin

MonkeeSage

12/15/2007 7:19:00 PM

0

On Dec 14, 4:41 pm, Junkone <junko...@gmail.com> wrote:
> hello
> I have the following 2 files
>
> Someopath/Dir1/File1.rb
> Someopath/Dir2/File2.rb
>
> How can i do a require 'relativepath/Dir2/File2.rb' from File1.rb
>
> appreciate any help.

$:.unshift(File.basename($0))
require "../Dir2/File2.rb"
require "../Dir3/File3.rb"
require "../Dir4/File4.rb"
....

Regards,
Jordan

MonkeeSage

12/15/2007 7:46:00 PM

0

On Dec 15, 1:19 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 14, 4:41 pm, Junkone <junko...@gmail.com> wrote:
>
> > hello
> > I have the following 2 files
>
> > Someopath/Dir1/File1.rb
> > Someopath/Dir2/File2.rb
>
> > How can i do a require 'relativepath/Dir2/File2.rb' from File1.rb
>
> > appreciate any help.
>
> $:.unshift(File.basename($0))
> require "../Dir2/File2.rb"
> require "../Dir3/File3.rb"
> require "../Dir4/File4.rb"
> ...
>
> Regards,
> Jordan

Oops. Those should have been "../../Dir2/File2.rb" &c.

MonkeeSage

12/15/2007 7:51:00 PM

0

On Dec 15, 1:19 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 14, 4:41 pm, Junkone <junko...@gmail.com> wrote:
>
> > hello
> > I have the following 2 files
>
> > Someopath/Dir1/File1.rb
> > Someopath/Dir2/File2.rb
>
> > How can i do a require 'relativepath/Dir2/File2.rb' from File1.rb
>
> > appreciate any help.
>
> $:.unshift(File.basename($0))
> require "../Dir2/File2.rb"
> require "../Dir3/File3.rb"
> require "../Dir4/File4.rb"
> ...
>
> Regards,
> Jordan

Heh...I'm not batting so well here. That should also have been
File.dirname, not basename! And probably the path should be expanded.
One more try here...

$:.unshift(File.expand_path(File.dirname($0)))
require "../../Dir2/File2.rb"
require "../../Dir3/File3.rb"
require "../../Dir4/File4.rb"
....

Regards,
Jordan