Loading configuration files in an OS-agnostic way

  • Thread starter Gavri Savio Fernandez
  • Start date
G

Gavri Savio Fernandez

Hi,
I downloaded ruby-google and tried to run the sample code when I noticed that the code was unusable as is, in Windows, because it assumed that the configuration file is named .google_key and Win 2000 Professional atleast does not allow '.' to be the first character of a filename.

KEY = File.open("#{ENV['HOME']}/.google_key") {|kf| kf.readline.chomp}

I made a quick modification just so I could get it working. (Ugly, I know)

KEY =
begin
File.open("#{ENV['HOME']}/.google_key") {|kf| kf.readline.chomp}
rescue
File.open("#{ENV['HOME']}/~google_key") {|kf| kf.readline.chomp}
end


What I'd like to know is what is the ruby idiom for choosing configuration files in an OS-agnostic way? (Emacs does this, choosing ~emacs on Win and .emacs on Linux) . Or is this too simple an issue to require an idiom?

thanks,
gavri
 
P

Peter Cooper

unusable as is, in Windows, because it assumed that the configuration
file is named .google_key and Win 2000 Professional atleast does not
allow '.' to be the first character of a filename.

Are you sure? That strikes me as odd, and Windows XP (which is
essentially 2000+goodies anyway) is fine with it. Most systems I have
encountered which are attempting to be agnostic use a period regardless
(or a tilde, as you have).

Pete
 
P

Pit Capitain

Peter said:
Are you sure? That strikes me as odd, and Windows XP (which is
essentially 2000+goodies anyway) is fine with it. Most systems I have
encountered which are attempting to be agnostic use a period regardless
(or a tilde, as you have).

At least on Win 2000, you can't create such a file in the Windows Explorer, but
you sure can in Ruby:

File.open ".google_key", "w" do |out| out.puts "# config" end

Regards,
Pit
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top