Reputation: 14016
How would I, say, determine if the file ~/.my_proj_config
exists on any OS in Ruby?
Upvotes: 16
Views: 6203
Reputation: 303224
This works in Ruby 1.9, but note that the call to expand_path
is required on some systems (e.g. Windows):
File.exists?( File.expand_path "~/.my_proj_config" )
Upvotes: 3
Reputation: 44952
A call to Dir.home is a OS independent way to get to the home directory for the user. You can then use it like
File.exists?(File.join(Dir.home, ".my_proj_config"))
Upvotes: 21
Reputation: 21979
Take a look at the Pathname
class, specifically the realpath
function - This will get you the full (expanded) path to your file.
http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/classes/Pathname.html#M001991
You then use the File
class along with exists?
method to find out if that exists. You shouldn't need to use realpath
if you use this method, however.
http://www.ruby-doc.org/core/classes/File.html#M000045
Upvotes: 0