是否有 Ruby 类/方法可以接受“完整路径”home/me/a_file.txt
并确定其是否是有效的文件路径?
# file? will only return true for files
File.file?(filename)
和
# Will also return true for directories - watch out!
File.exist?(filename)
>> File.file?('/etc/passwd')
=> true
>> File.file?('/etc/')
=> false
>> File.exists?('/etc/')
=> true
>> File.exists?('/etc/passwd')
=> true
>> File.file?('/etc/passwd') and File.exists?('/etc/passwd')
- shadowbq请查看Pathname,特别是Pathname#exist?
方法。