问题
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])
?
回答1:
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 as realpath
will throw an exception.
For example, when getting the path of a file that does not exist:
irb(main):001:0> File.realpath('nonexistent.file')
Errno::ENOENT: No such file or directory - H:/nonexistent.file
from (irb):1:in `realpath'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):002:0> File.realdirpath('nonexistent.file')
=> "H:/nonexistent.file"
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:
- You are running irb from the root of your H: drive (ie this is your current directory)
- You have a sub-folder named 'Folder' (ie H:\Folder)
Then you can see the difference:
H:\>irb
irb(main):001:0> File.realdirpath('test.txt')
=> "H:/test.txt"
irb(main):002:0> File.realdirpath('test.txt', './Expense')
=> "H:/Expense/test.txt"
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.
回答2:
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:
Ruby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename “/gumby/ruby/test.rb” will be opened as “\gumby\ruby\test.rb”. When specifying a Windows-style filename in a Ruby string, remember to escape the backslashes:
"c:\\gumby\\ruby\\test.rb"
Our examples here will use the Unix-style forward slashes; File::SEPARATOR can be used to get the platform-specific separator character.
来源:https://stackoverflow.com/questions/14569044/confusion-with-ruby-file-class-related-path-methods