Relative Path in ruby - for File open

S

Shekar Ls

Hi Guys,
I am trying to fetch a file to do some processing in ruby,
instead of giving the absolute path i wanted to give relative path.

File.open("config.txt") do |source|
source.each_line do |line|
if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
....

In the above mentioned example, i want config.txt to be accessed from
the current path whichever user runs it.

Cheers
 
R

Ryan Davis

Hi Guys,
I am trying to fetch a file to do some processing in ruby,
instead of giving the absolute path i wanted to give relative path.

File.open("config.txt") do |source|
source.each_line do |line|
if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
....

In the above mentioned example, i want config.txt to be accessed from
the current path whichever user runs it.

what's the problem?
 
B

Ben Giddings

In the above mentioned example, i want config.txt to be accessed from
the current path whichever user runs it.

By "current path" do you mean executable path or the current working
directory of the script?

If you mean the current working directory (i.e. ENV['PWD']) everything
should work as you expect. Just do File.open("config.txt")

Ben
 
B

Brian Candler

Ben said:
In the above mentioned example, i want config.txt to be accessed from
the current path whichever user runs it.

By "current path" do you mean executable path or the current working
directory of the script?

If you mean the current working directory (i.e. ENV['PWD']) everything
should work as you expect. Just do File.open("config.txt")

And if you mean the directory in which the script is located, then

AppRoot = File.expand_path(File.dirname(__FILE__))

File.open(File.join(AppRoot, "config.txt")) do |source|
..
end
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top