doing require on some relateive path

J

Junkone

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

John Pywtorak

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
 
V

vinbarnes

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.rubyforge.org,
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
 
M

MonkeeSage

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
 
M

MonkeeSage

$:.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.
 
M

MonkeeSage

$:.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
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top