Ruby的目录vs文件vs路径名?

前端之家收集整理的这篇文章主要介绍了Ruby的目录vs文件vs路径名?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Ruby的Dir,File和Pathname类有什么区别?他们似乎分享了常用的方法,如basename,dirname,glob和join.什么时候使用一个有利于另一个?

将其功能与Dir和File进行比较时,路径名似乎特别奇怪.

解决方法

根据Ruby文档 Dir,FilePathname,他们肯定有很多共同之处.

Dir和File之间的原理似乎是Dir假定它正在使用的对象是一个目录,File假定文件.对于大多数目的,它们显然可以互换使用,但即使代码有效,如果使用Dir操作文件文件来操作目录,那么读取代码的人也可能会感到困惑.

Pathname看起来是一种多操作系统的方法来定位文件和目录.由于Windows和* nix机器以不同的方式处理文件管理,如果希望脚本在任何地方运行,可能会以特定于操作系统的方式来引用文件或目录.从文档:

Pathname represents a pathname which locates a file in a filesystem. The pathname depends on OS: Unix,Windows,etc. Pathname library works with pathnames of local OS. However non-Unix pathnames are supported experimentally.

It does not represent the file itself. A Pathname can be relative or absolute. It’s not until you try to reference the file that it even matters whether the file exists or not.

Pathname is immutable. It has no method for destructive update.

希望这可以帮助.

原文链接:https://www.f2er.com/ruby/272935.html

猜你在找的Ruby相关文章