Doing require on some relateive path

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.

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

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

On Dec 14, 4:41 pm, Junkone [email protected] 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

On Dec 15, 1:19 pm, MonkeeSage [email protected] wrote:

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

On Dec 15, 1:19 pm, MonkeeSage [email protected] wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs