require and relative paths

F

francisrammeloo

Hi all,

Suppose I have following code:

require "./Classes/Printer" # My personal printer class


Now if I want to run my program Ruby needs to find the Printer.rb file
in the Classes directory. But if the script was run like this:
cd ~
ruby ~/rubydev/projects/texteditor/main.rb


Then the current path will be ~ and Ruby won't find
../Classes/Printer.rb
How do I solve this problem?


Greetings,
Francis
 
R

Robert Klemme

Hi all,

Suppose I have following code:

require "./Classes/Printer" # My personal printer class


Now if I want to run my program Ruby needs to find the Printer.rb file
in the Classes directory. But if the script was run like this:



Then the current path will be ~ and Ruby won't find
./Classes/Printer.rb
How do I solve this problem?

Did you try removing "./"? Other than that, if directory "Classes" is
always relative to the script, you can use "require
File.join(File.basename($0), "Classes", "Printer")". Btw, I think you
don't need the extension, Ruby will figure automatically.

Kind regards

robert
 
J

Jim Menard

Hi all,

Suppose I have following code:

require "./Classes/Printer" # My personal printer class


Now if I want to run my program Ruby needs to find the Printer.rb file
in the Classes directory. But if the script was run like this:





Then the current path will be ~ and Ruby won't find
../Classes/Printer.rb
How do I solve this problem?

require File.join(File.dirname(__FILE__), 'Classes', 'Printer')

Jim
 
F

francisrammeloo

I tried to remove the "./" but that doesn't work.

Using the File.basename($0) trick gives following result:
LoadError: No such file to load -- irb/Classes/BobParser

Perhaps I should mention that I use following emacs command: C-c C-l
and that I'm pretty new to emacs : )
 
R

Robert Klemme

I tried to remove the "./" but that doesn't work.

Using the File.basename($0) trick gives following result:
LoadError: No such file to load -- irb/Classes/BobParser

Of course this won't work if you try it from irb. Did you try it in your
script?
Perhaps I should mention that I use following emacs command: C-c C-l
and that I'm pretty new to emacs : )

Regards

robert
 
E

Erik Veenstra

require "./Classes/Printer" # My personal printer class
Now if I want to run my program Ruby needs to find the
Printer.rb file in the Classes directory. But if the script
was run like this:

$ cd ~
$ ruby ~/rubydev/projects/texteditor/main.rb

Then the current path will be ~ and Ruby won't find
./Classes/Printer.rb

How do I solve this problem?

$: << File.dirname(File.expand_path(__FILE__))
require "Classes/Printer"

Skip the "./" part. "." is already added to $:

gegroet,
Erik V. - http://www.erikveen.dds.nl/
 
F

francisrammeloo

require File.join(File.dirname(__FILE__), 'Classes', 'Printer') works
thanks!


I'll try Erik's trick tomorrow. His' would be even nicer.


Best regards,
Francis
 
F

francisrammeloo

Thank you very much.

This works, but not completely...
Suppose I have this code:

$: << File.dirname(File.expand_path(__FILE__))
require "Classes/Window"
require "../Modules/InstanceCounter"

Now ruby finds "Classes/Window", but it doesn't find
"../Modules/InstanceCounter".

Is this normal?
Is there a solution?

Many thanks in advance for all helpful hints.

Best regards,
Francis
 
F

francisrammeloo

I found a solution, but I think it's a bit clumpsy:

$: << File.dirname(File.expand_path(__FILE__))
$: << File.dirname(File.expand_path(__FILE__)).sub(/(.*)\/\w+/,
'\1') <-- added
require "Classes/Window"
require "Modules/InstanceCounter" <-- no "../" needed anymore


Best regards,
Francis
 
E

Erik Veenstra

I found a solution, but I think it's a bit clumpsy:
$: << File.dirname(File.expand_path(__FILE__))
$: << File.dirname(File.expand_path(__FILE__)).sub(/(.*)\/\w+/, '\1')
require "Classes/Window"
require "Modules/InstanceCounter" <-- no "../" needed anymore

I couldn't do it clumpsier...:)

This is what I would do:

f1 = File.expand_path(__FILE__)
d1 = File.dirname(f1)
d2 = File.dirname(d1)
$: << d1
$: << d2
require "Classes/Window"
require "Modules/InstanceCounter"

If you want to add more directories, upto the root, use this:

entry = File.expand_path(__FILE__)
while (not $:.include?(entry = File.dirname(entry)))
$: << entry
end
require "Classes/Window"
require "Modules/InstanceCounter"

(Just kidding...)

Just don't use "./" or "../" or any other "relative" references
to a file in a require statement. Trying setting up $:
correctly instead and feed require the name of the library
instead of filenames.

gegroet,
Erik V. - http://www.erikveen.dds.nl/
 
F

francisrammeloo

Thanks, that looks a lot better than my "solution"..

vriendelijke groeten,
Francis
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top