I am getting confused with the utility of the File Class
methods as below:
1. File::absolute_path
2. File::realdirpath
3. File::realpath
4. File::expand_path
What I tried below:
irb(main):001:0> Dir.pwd
=> "C:/Users/Matt"
irb(main):002:0> Dir.chdir('D:\VB Script\excel_ie_wsh')
=> 0
irb(main):003:0> Dir.pwd
=> "D:/VB Script/excel_ie_wsh"
irb(main):005:0> File.realdirpath('\VB Script\excel_ie_wsh')
=> "/VB Script/excel_ie_wsh"
irb(main):006:0> File.realpath('\VB Script\excel_ie_wsh')
=> "/VB Script/excel_ie_wsh"
irb(main):007:0> File.absolute_path('\VB Script\excel_ie_wsh')
=> "D:/VB Script/excel_ie_wsh"
irb(main):008:0> File.realpath('readme.txt')
=> "D:/VB Script/excel_ie_wsh/readme.txt"
irb(main):009:0> File.realdirpath('readme.txt')
=> "D:/VB Script/excel_ie_wsh/readme.txt"
irb(main):012:0> File.absolute_path('readme.txt')
=> "D:/VB Script/excel_ie_wsh/readme.txt"
irb(main):013:0>
Questions:
- Why
File.realdirpath
andFile.realpath
produces the same output? - How
File.realpath
differs fromFile.absolute_path
? - How does
absolute_path(file_nam)
differ fromabsolute_path(file_name[, dir_string] )
? - How does
realdirpath(pathname)
differ fromrealdirpath(pathname [, dir_string])
?
Why File.realdirpath and File.realpath produces the same output?
As per the ruby-doc, the only difference between these two methods is whether or not the last component of the pathname must exist.
realdirpath
does not care, where asrealpath
will throw an exception.For example, when getting the path of a file that does not exist:
How File.realpath differs from File.absolute_path?
I am not on a UNIX system to test this, but my guess is that the only difference is when the path starts the tilde (ie ~). In UNIX, the tilde represents the home directory. I assume the difference between the two methods is whether or not it expands the home directory or not.
If you are on windows, it should not matter (ie there is no home directory shortcut).
How does realdirpath(pathname) differ from realdirpath(pathname [, dir_string]) ?
The ruby-doc states "If dir_string is given, it is used as a base directory for interpreting relative pathname instead of the current directory." In other words, dir_string can be specified to override the starting point.
For example, let us assume that:
Then you can see the difference:
You can see that the second statement locates the file with respect to the directory stated in dir_string.
How does absolute_path(file_nam) differ from absolute_path(file_name[, dir_string] )?
This is the same as the previous question.
This is not to answer your question, it's to help preserve your sanity as you work with languages like Ruby, Perl, and probably Python, on a Windows box.
Don't waste your time writing your paths using back-slashes. Instead, join the *nix-type OSes and use forward slashes in your code. This will make it easier to move files from a desktop environment to a networking/hosting environment since the big-iron on the internet and enterprises is usually a *nix system.
The IO documentation says: