class Pathname
pathname.rb
面向对象的 Pathname 类
- 作者
-
Tanaka Akira <akr@m17n.org>
- 文档
-
作者和 Gavin Sinclair
有关文档,请参阅类 Pathname。
Pathname 代表文件系统上的文件或目录的名称,但不是文件本身。
路径名取决于操作系统:Unix、Windows 等。此库适用于本地操作系统的路径名,但对非 Unix 路径名提供实验性支持。
一个 Pathname 可以是相对的或绝对的。直到您尝试引用文件时,文件是否存在才变得重要。
Pathname 是不可变的。它没有任何破坏性更新的方法。
此类旨在比标准 Ruby 提供更简洁的文件路径信息操作方式。下面的示例演示了其中的区别。
来自 File、FileTest 以及部分来自 Dir 和 FileUtils 的**所有**功能都以一种意想不到的方式包含在内。它本质上是所有这些以及更多功能的门面。
示例
示例 1:使用 Pathname
require 'pathname' pn = Pathname.new("/usr/bin/ruby") size = pn.size # 27662 isdir = pn.directory? # false dir = pn.dirname # Pathname:/usr/bin base = pn.basename # Pathname:ruby dir, base = pn.split # [Pathname:/usr/bin, Pathname:ruby] data = pn.read pn.open { |f| _ } pn.each_line { |line| _ }
示例 2:使用标准 Ruby
pn = "/usr/bin/ruby" size = File.size(pn) # 27662 isdir = File.directory?(pn) # false dir = File.dirname(pn) # "/usr/bin" base = File.basename(pn) # "ruby" dir, base = File.split(pn) # ["/usr/bin", "ruby"] data = File.read(pn) File.open(pn) { |f| _ } File.foreach(pn) { |line| _ }
示例 3:特殊功能
p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8 p3 = p1.parent # Pathname:/usr p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8 pwd = Pathname.pwd # Pathname:/home/gavin pwd.absolute? # true p5 = Pathname.new "." # Pathname:. p5 = p5 + "music/../articles" # Pathname:music/../articles p5.cleanpath # Pathname:articles p5.realpath # Pathname:/home/gavin/articles p5.children # [Pathname:/home/gavin/articles/linux, ...]
功能细分
核心方法
这些方法本质上是在操作一个 String,因为路径本身就是字符串。这些方法除了 mountpoint?、children、each_child、realdirpath 和 realpath 之外,都不会访问文件系统。
-
+
File 状态谓词方法
这些方法是 FileTest 的门面
File 属性和操作方法
这些方法是 File 的门面
目录方法
这些方法是 Dir 的门面
-
each_entry(&block)
实用工具
方法文档
如上一节所示,Pathname 中的大多数方法都是门面。这些方法的文档通常只会说,例如,“参见 FileTest.writable?”,因为您应该已经熟悉原始方法,并且其文档(例如通过 ri)会包含更多信息。在某些情况下,会跟有简要说明。
Constants
Public Class Methods
Source
# File pathname_builtin.rb, line 1096 def Pathname.glob(*args, **kwargs) # :yield: pathname if block_given? Dir.glob(*args, **kwargs) {|f| yield self.new(f) } else Dir.glob(*args, **kwargs).map {|f| self.new(f) } end end
Source
# File lib/pathname.rb, line 63 def self.mktmpdir require 'tmpdir' unless defined?(Dir.mktmpdir) if block_given? Dir.mktmpdir do |dir| dir = self.new(dir) yield dir end else self.new(Dir.mktmpdir) end end
Source
# File pathname_builtin.rb, line 214 def initialize(path) @path = File.path(path).dup rescue TypeError => e raise e.class, "Pathname.new requires a String, #to_path or #to_str", cause: nil end
从给定的 String(或类 String 对象)创建一个 Pathname 对象。如果 `path` 包含 NUL 字符 (`\0`),则会引发 ArgumentError。
Public Instance Methods
Source
# File pathname_builtin.rb, line 665 def +(other) other = Pathname.new(other) unless Pathname === other Pathname.new(plus(@path, other.path)) end
将一个路径片段追加到 `self` 以生成一个新的 Pathname 对象。由于 `other` 被视为相对于 `self` 的路径,因此如果 `other` 是一个绝对路径,则新的 Pathname 对象将仅从 `other` 创建。
p1 = Pathname.new("/usr") # Pathname:/usr p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd # / is aliased to +. p4 = p1 / "bin/ruby" # Pathname:/usr/bin/ruby p5 = p1 / "/etc/passwd" # Pathname:/etc/passwd
此方法不访问文件系统;它是纯字符串操作。
Source
static VALUE
path_cmp(VALUE self, VALUE other)
{
VALUE s1, s2;
char *p1, *p2;
char *e1, *e2;
if (!rb_obj_is_kind_of(other, rb_cPathname))
return Qnil;
s1 = get_strpath(self);
s2 = get_strpath(other);
p1 = RSTRING_PTR(s1);
p2 = RSTRING_PTR(s2);
e1 = p1 + RSTRING_LEN(s1);
e2 = p2 + RSTRING_LEN(s2);
while (p1 < e1 && p2 < e2) {
int c1, c2;
c1 = (unsigned char)*p1++;
c2 = (unsigned char)*p2++;
if (c1 == '/') c1 = '\0';
if (c2 == '/') c2 = '\0';
if (c1 != c2) {
if (c1 < c2)
return INT2FIX(-1);
else
return INT2FIX(1);
}
}
if (p1 < e1)
return INT2FIX(1);
if (p2 < e2)
return INT2FIX(-1);
return INT2FIX(0);
}
为路径名提供区分大小写的比较运算符。
Pathname.new('/usr') <=> Pathname.new('/usr/bin') #=> -1 Pathname.new('/usr/bin') <=> Pathname.new('/usr/bin') #=> 0 Pathname.new('/usr/bin') <=> Pathname.new('/USR/BIN') #=> 1
它将根据左参数相对于右参数的值返回 -1、0 或 1。如果参数不可比较,它将返回 nil。
Source
# File pathname_builtin.rb, line 234 def ==(other) return false unless Pathname === other other.path == @path end
将此路径名与 `other` 进行比较。比较是基于字符串的。请注意,两个不同的路径(`foo.txt` 和 `./foo.txt`)可以指向同一个文件。
Source
# File pathname_builtin.rb, line 542 def absolute? ABSOLUTE_PATH.match? @path end
用于测试路径是否为绝对路径的谓词方法。
如果路径名以斜杠开头,则返回 true。
p = Pathname.new('/im/sure') p.absolute? #=> true p = Pathname.new('not/so/sure') p.absolute? #=> false
Source
# File pathname_builtin.rb, line 639 def ascend return to_enum(__method__) unless block_given? path = @path yield self while r = chop_basename(path) path, = r break if path.empty? yield self.class.new(del_trailing_separator(path)) end end
按升序遍历给定路径中的每个元素,并为每个元素生成一个新的 Pathname 对象。
Pathname.new('/path/to/some/file.rb').ascend {|v| p v} #<Pathname:/path/to/some/file.rb> #<Pathname:/path/to/some> #<Pathname:/path/to> #<Pathname:/path> #<Pathname:/> Pathname.new('path/to/some/file.rb').ascend {|v| p v} #<Pathname:path/to/some/file.rb> #<Pathname:path/to/some> #<Pathname:path/to> #<Pathname:path>
如果没有块,则返回一个 Enumerator。
enum = Pathname.new("/usr/bin/ruby").ascend
# ... do stuff ...
enum.each { |e| ... }
# yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
它不访问文件系统。
Source
# File pathname_builtin.rb, line 898 def atime() File.atime(@path) end
参见 File.atime。返回上次访问时间。
Source
# File pathname_builtin.rb, line 972 def basename(...) self.class.new(File.basename(@path, ...)) end
参见 File.basename。返回路径的最后一个组件。
Source
# File pathname_builtin.rb, line 881 def binread(...) File.binread(@path, ...) end
参见 File.binread。返回文件中的所有字节,或指定的第一个 N 字节。
Source
# File pathname_builtin.rb, line 895 def binwrite(...) File.binwrite(@path, ...) end
以二进制模式打开文件并写入 contents。
参见 File.binwrite。
Source
# File pathname_builtin.rb, line 904 def birthtime() File.birthtime(@path) end
返回文件的创建时间。如果平台没有创建时间,则引发 NotImplementedError。
参见 File.birthtime。
Source
# File pathname_builtin.rb, line 1010 def blockdev?() FileTest.blockdev?(@path) end
Source
# File pathname_builtin.rb, line 1013 def chardev?() FileTest.chardev?(@path) end
Source
# File pathname_builtin.rb, line 758 def children(with_directory=true) with_directory = false if @path == '.' result = [] Dir.foreach(@path) {|e| next if e == '.' || e == '..' if with_directory result << self.class.new(File.join(@path, e)) else result << self.class.new(e) end } result end
将目录的子项(文件和子目录,非递归)作为 Pathname 对象数组返回。
默认情况下,返回的路径名将包含访问文件的足够信息。如果将 with_directory 设置为 false,则返回的路径名将仅包含文件名。
例如
pn = Pathname("/usr/lib/ruby/1.8")
pn.children
# -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
Pathname:/usr/lib/ruby/1.8/Env.rb,
Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
pn.children(false)
# -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
请注意,结果中永远不会包含目录中的 . 和 .. 条目,因为它们不是子项。
Source
# File pathname_builtin.rb, line 913 def chmod(mode) File.chmod(mode, @path) end
参见 File.chmod。更改权限。
Source
# File pathname_builtin.rb, line 919 def chown(owner, group) File.chown(owner, group, @path) end
参见 File.chown。更改文件的所有者和组。
Source
# File pathname_builtin.rb, line 403 def cleanpath(consider_symlink=false) if consider_symlink cleanpath_conservative else cleanpath_aggressive end end
返回 self 的清理后的路径名,删除连续斜杠和无效的点。不访问文件系统。
如果 consider_symlink 为 true,则使用更保守的算法来避免破坏符号链接。这可能会保留比绝对必要更多的 .. 条目,但如果不访问文件系统,这是无法避免的。
Source
# File pathname_builtin.rb, line 907 def ctime() File.ctime(@path) end
参见 File.ctime。返回上次(目录条目,非文件)更改时间。
Source
# File pathname_builtin.rb, line 606 def descend return to_enum(__method__) unless block_given? vs = [] ascend {|v| vs << v } vs.reverse_each {|v| yield v } nil end
按降序遍历给定路径中的每个元素,并为每个元素生成一个新的 Pathname 对象。
Pathname.new('/path/to/some/file.rb').descend {|v| p v} #<Pathname:/> #<Pathname:/path> #<Pathname:/path/to> #<Pathname:/path/to/some> #<Pathname:/path/to/some/file.rb> Pathname.new('path/to/some/file.rb').descend {|v| p v} #<Pathname:path> #<Pathname:path/to> #<Pathname:path/to/some> #<Pathname:path/to/some/file.rb>
如果没有块,则返回一个 Enumerator。
enum = Pathname.new("/usr/bin/ruby").descend
# ... do stuff ...
enum.each { |e| ... }
# yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
它不访问文件系统。
Source
# File pathname_builtin.rb, line 1039 def directory?() FileTest.directory?(@path) end
Source
# File pathname_builtin.rb, line 975 def dirname() self.class.new(File.dirname(@path)) end
参见 File.dirname。返回路径的除最后一个组件以外的所有部分。
Source
# File pathname_builtin.rb, line 808 def each_child(with_directory=true, &b) children(with_directory).each(&b) end
遍历目录的子项(文件和子目录,非递归)。
它为每个子项迭代生成一个 Pathname 对象。
默认情况下,生成的路径名将包含访问文件的足够信息。
如果将 with_directory 设置为 false,则生成的路径名将仅包含文件名。
Pathname("/usr/local").each_child {|f| p f } #=> #<Pathname:/usr/local/share> # #<Pathname:/usr/local/bin> # #<Pathname:/usr/local/games> # #<Pathname:/usr/local/lib> # #<Pathname:/usr/local/include> # #<Pathname:/usr/local/sbin> # #<Pathname:/usr/local/src> # #<Pathname:/usr/local/man> Pathname("/usr/local").each_child(false) {|f| p f } #=> #<Pathname:share> # #<Pathname:bin> # #<Pathname:games> # #<Pathname:lib> # #<Pathname:include> # #<Pathname:sbin> # #<Pathname:src> # #<Pathname:man>
请注意,结果中永远不会包含目录中的 . 和 .. 条目,因为它们不是子项。
Source
# File pathname_builtin.rb, line 1133 def each_entry(&block) # :yield: pathname return to_enum(__method__) unless block_given? Dir.foreach(@path) {|f| yield self.class.new(f) } end
遍历目录中的条目(文件和子目录)。它为每个条目迭代生成一个 Pathname 对象。
此方法自 1.8.1 起可用。
Source
# File pathname_builtin.rb, line 574 def each_filename # :yield: filename return to_enum(__method__) unless block_given? _, names = split_names(@path) names.each {|filename| yield filename } nil end
遍历路径的每个组件。
Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
# yields "usr", "bin", and "ruby".
如果没有块,则返回一个 Enumerator。
enum = Pathname.new("/usr/bin/ruby").each_filename
# ... do stuff ...
enum.each { |e| ... }
# yields "usr", "bin", and "ruby".
Source
# File pathname_builtin.rb, line 871 def each_line(...) # :yield: line File.foreach(@path, ...) end
Source
# File pathname_builtin.rb, line 1018 def empty? if FileTest.directory?(@path) Dir.empty?(@path) else File.empty?(@path) end end
测试文件是否为空。
参见 Dir#empty? 和 FileTest.empty?。
Source
# File pathname_builtin.rb, line 1127 def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
返回目录中的条目(文件和子目录),每个条目都作为 Pathname 对象。
Source
# File pathname_builtin.rb, line 1027 def executable?() FileTest.executable?(@path) end
Source
# File pathname_builtin.rb, line 1030 def executable_real?() FileTest.executable_real?(@path) end
Source
# File pathname_builtin.rb, line 1033 def exist?() FileTest.exist?(@path) end
参见 FileTest.exist?。
Source
# File pathname_builtin.rb, line 981 def expand_path(...) self.class.new(File.expand_path(@path, ...)) end
参见 File.expand_path。
Source
# File pathname_builtin.rb, line 978 def extname() File.extname(@path) end
参见 File.extname。返回文件的扩展名。
Source
# File pathname_builtin.rb, line 1042 def file?() FileTest.file?(@path) end
参见 FileTest.file?。
Source
# File lib/pathname.rb, line 30 def find(ignore_error: true) # :yield: pathname return to_enum(__method__, ignore_error: ignore_error) unless block_given? require 'find' if @path == '.' Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f.delete_prefix('./')) } else Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f) } end end
以深度优先的方式遍历目录树,为“此”目录下的每个文件生成一个 Pathname。
请注意,您需要 `require 'pathname'` 才能使用此方法。
Returns an Enumerator if no block is given.
由于它是由标准库模块 Find 实现的,因此可以使用 Find.prune 来控制遍历。
如果 `self` 是 `.`,则生成的路径名以当前目录中的文件名开头,而不是 `./`。
参见 Find.find
Source
# File pathname_builtin.rb, line 926 def fnmatch(pattern, ...) File.fnmatch(pattern, @path, ...) end
参见 File.fnmatch。如果接收者匹配给定模式,则返回 true。
Source
# File pathname_builtin.rb, line 929 def fnmatch?(pattern, ...) File.fnmatch?(pattern, @path, ...) end
参见 File.fnmatch?(与 fnmatch 相同)。
Source
# File pathname_builtin.rb, line 223 def freeze super @path.freeze self end
冻结 self。
Object#freezeSource
# File pathname_builtin.rb, line 933 def ftype() File.ftype(@path) end
参见 File.ftype。返回文件的“类型”(“file”、“directory”等)。
Source
# File pathname_builtin.rb, line 1111 def glob(*args, **kwargs) # :yield: pathname if block_given? Dir.glob(*args, **kwargs, base: @path) {|f| yield self + f } else Dir.glob(*args, **kwargs, base: @path).map {|f| self + f } end end
Source
# File pathname_builtin.rb, line 1036 def grpowned?() FileTest.grpowned?(@path) end
Source
# File pathname_builtin.rb, line 725 def join(*args) return self if args.empty? result = args.pop result = Pathname.new(result) unless Pathname === result return result if result.absolute? args.reverse_each {|arg| arg = Pathname.new(arg) unless Pathname === arg result = arg + result return result if result.absolute? } self + result end
将给定的路径名连接到 `self` 以创建一个新的 Pathname 对象。这实际上与使用 Pathname#+ 顺序追加 `self` 和所有参数相同。
path0 = Pathname.new("/usr") # Pathname:/usr path0 = path0.join("bin/ruby") # Pathname:/usr/bin/ruby # is the same as path1 = Pathname.new("/usr") + "bin/ruby" # Pathname:/usr/bin/ruby path0 == path1 #=> true
Source
# File pathname_builtin.rb, line 916 def lchmod(mode) File.lchmod(mode, @path) end
参见 File.lchmod。
Source
# File pathname_builtin.rb, line 922 def lchown(owner, group) File.lchown(owner, group, @path) end
参见 File.lchown。
Source
# File pathname_builtin.rb, line 969 def lutime(atime, mtime) File.lutime(atime, mtime, @path) end
Source
# File pathname_builtin.rb, line 936 def make_link(old) File.link(old, @path) end
参见 File.link。创建硬链接。
Source
# File pathname_builtin.rb, line 956 def make_symlink(old) File.symlink(old, @path) end
参见 File.symlink。创建符号链接。
Source
# File pathname_builtin.rb, line 1139 def mkdir(...) Dir.mkdir(@path, ...) end
参见 Dir.mkdir。创建引用的目录。
Source
# File pathname_builtin.rb, line 334 def mkpath(mode: nil) path = @path == '/' ? @path : @path.chomp('/') stack = [] until File.directory?(path) || File.dirname(path) == path stack.push path path = File.dirname(path) end stack.reverse_each do |dir| dir = dir == '/' ? dir : dir.chomp('/') if mode Dir.mkdir dir, mode File.chmod mode, dir else Dir.mkdir dir end rescue SystemCallError raise unless File.directory?(dir) end self end
创建完整的路径,包括任何尚不存在的中间目录。
Source
# File pathname_builtin.rb, line 510 def mountpoint? begin stat1 = self.lstat stat2 = self.parent.lstat stat1.dev != stat2.dev || stat1.ino == stat2.ino rescue Errno::ENOENT false end end
如果 `self` 指向一个挂载点,则返回 true。
Source
# File pathname_builtin.rb, line 910 def mtime() File.mtime(@path) end
参见 File.mtime。返回上次修改时间。
Source
# File pathname_builtin.rb, line 939 def open(...) # :yield: file File.open(@path, ...) end
参见 File.open。打开文件进行读写。
Source
# File pathname_builtin.rb, line 1145 def opendir(&block) # :yield: dir Dir.open(@path, &block) end
参见 Dir.open。
Source
# File pathname_builtin.rb, line 1051 def owned?() FileTest.owned?(@path) end
参见 FileTest.owned?。
Source
# File pathname_builtin.rb, line 505 def parent self + '..' end
返回父目录。
这与 `self + '..'` 相同。
Source
# File pathname_builtin.rb, line 1045 def pipe?() FileTest.pipe?(@path) end
参见 FileTest.pipe?。
Source
# File pathname_builtin.rb, line 877 def read(...) File.read(@path, ...) end
参见 File.read。返回文件中的所有数据,或指定的第一个 N 字节。
Source
# File pathname_builtin.rb, line 1054 def readable?() FileTest.readable?(@path) end
Source
# File pathname_builtin.rb, line 1060 def readable_real?() FileTest.readable_real?(@path) end
Source
# File pathname_builtin.rb, line 884 def readlines(...) File.readlines(@path, ...) end
参见 File.readlines。返回文件中的所有行。
Source
# File pathname_builtin.rb, line 944 def readlink() self.class.new(File.readlink(@path)) end
参见 File.readlink。读取符号链接。
Source
# File pathname_builtin.rb, line 1003 def realdirpath(...) self.class.new(File.realdirpath(@path, ...)) end
返回实际文件系统中的 self 的真实(绝对)路径名。
不包含符号链接或无效的点、.. 和 .。
真实路径名的最后一个组件可能不存在。
Source
# File pathname_builtin.rb, line 996 def realpath(...) self.class.new(File.realpath(@path, ...)) end
返回实际文件系统中的 self 的真实(绝对)路径名。
不包含符号链接或无效的点、.. 和 .。
调用此方法时,路径名的所有组件都必须存在。
Source
# File pathname_builtin.rb, line 557 def relative? !absolute? end
如果路径名以斜杠开头,则返回 false。
p = Pathname.new('/im/sure') p.relative? #=> false p = Pathname.new('not/so/sure') p.relative? #=> true
Source
# File pathname_builtin.rb, line 826 def relative_path_from(base_directory) base_directory = Pathname.new(base_directory) unless base_directory.is_a? Pathname dest_directory = self.cleanpath.path base_directory = base_directory.cleanpath.path dest_prefix = dest_directory dest_names = [] while r = chop_basename(dest_prefix) dest_prefix, basename = r dest_names.unshift basename if basename != '.' end base_prefix = base_directory base_names = [] while r = chop_basename(base_prefix) base_prefix, basename = r base_names.unshift basename if basename != '.' end unless same_paths?(dest_prefix, base_prefix) raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}" end while !dest_names.empty? && !base_names.empty? && same_paths?(dest_names.first, base_names.first) dest_names.shift base_names.shift end if base_names.include? '..' raise ArgumentError, "base_directory has ..: #{base_directory.inspect}" end base_names.fill('..') relpath_names = base_names + dest_names if relpath_names.empty? Pathname.new('.') else Pathname.new(File.join(*relpath_names)) end end
从给定的 base_directory 返回到接收者的相对路径。
如果 `self` 是绝对的,则 `base_directory` 也必须是绝对的。
如果 `self` 是相对的,则 `base_directory` 也必须是相对的。
此方法不访问文件系统。它假定没有符号链接。
当找不到相对路径时,会引发 ArgumentError。
请注意,此方法不处理文件系统的大小写敏感性与操作系统默认值不同的情况。
Source
# File pathname_builtin.rb, line 947 def rename(to) File.rename(@path, to) end
参见 File.rename。重命名文件。
Source
# File lib/pathname.rb, line 48 def rmtree(noop: nil, verbose: nil, secure: nil) # The name "rmtree" is borrowed from File::Path of Perl. # File::Path provides "mkpath" and "rmtree". require 'fileutils' FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure) self end
Source
# File pathname_builtin.rb, line 527 def root? chop_basename(@path) == nil && SEPARATOR_PAT.match?(@path) end
根目录的谓词方法。如果路径名由连续的斜杠组成,则返回 true。
它不访问文件系统。因此,对于某些指向根目录的路径名(如 /usr/..),它可能返回 false。
Source
# File pathname_builtin.rb, line 1066 def setgid?() FileTest.setgid?(@path) end
参见 FileTest.setgid?。
Source
# File pathname_builtin.rb, line 1063 def setuid?() FileTest.setuid?(@path) end
参见 FileTest.setuid?。
Source
# File pathname_builtin.rb, line 1072 def size?() FileTest.size?(@path) end
参见 FileTest.size?。
Source
# File pathname_builtin.rb, line 1048 def socket?() FileTest.socket?(@path) end
参见 FileTest.socket?。
Source
# File pathname_builtin.rb, line 985 def split() array = File.split(@path) raise TypeError, 'wrong argument type nil (expected Array)' unless Array === array array.map {|f| self.class.new(f) } end
参见 File.split。返回 dirname 和 basename 组成的 Array。
Source
# File pathname_builtin.rb, line 950 def stat() File.stat(@path) end
参见 File.stat。返回一个 File::Stat 对象。
Source
# File pathname_builtin.rb, line 1075 def sticky?() FileTest.sticky?(@path) end
参见 FileTest.sticky?。
Source
static VALUE
path_sub(int argc, VALUE *argv, VALUE self)
{
VALUE str = get_strpath(self);
if (rb_block_given_p()) {
str = rb_block_call(str, id_sub, argc, argv, 0, 0);
}
else {
str = rb_funcallv(str, id_sub, argc, argv);
}
return rb_class_new_instance(1, &str, rb_obj_class(self));
}
使用 String#sub 替换后返回一个路径名。
path1 = Pathname.new('/usr/bin/perl') path1.sub('perl', 'ruby') #=> #<Pathname:/usr/bin/ruby>
Source
# File pathname_builtin.rb, line 292 def sub_ext(repl) ext = File.extname(@path) # File.extname("foo.bar:stream") returns ".bar" on NTFS and not ".bar:stream" # (see ruby_enc_find_extname()). # The behavior of Pathname#sub_ext is to replace everything # from the start of the extname until the end of the path with repl. unless @path.end_with?(ext) ext = @path[@path.rindex(ext)..] end self.class.new(@path.chomp(ext) + repl) end
返回一个路径名,其中 `repl` 作为后缀附加到基本名称。
如果 self 没有扩展名部分,则附加 repl。
Pathname.new('/usr/bin/shutdown').sub_ext('.rb') #=> #<Pathname:/usr/bin/shutdown.rb>
Source
# File pathname_builtin.rb, line 1078 def symlink?() FileTest.symlink?(@path) end
Source
# File pathname_builtin.rb, line 887 def sysopen(...) File.sysopen(@path, ...) end
参见 File.sysopen。
Source
# File pathname_builtin.rb, line 959 def truncate(length) File.truncate(@path, length) end
参见 File.truncate。将文件截断为 length 字节。
Source
# File pathname_builtin.rb, line 1153 def unlink() Dir.unlink @path rescue Errno::ENOTDIR File.unlink @path end
删除文件或目录,根据需要使用 File.unlink 或 Dir.unlink。
Source
# File pathname_builtin.rb, line 962 def utime(atime, mtime) File.utime(atime, mtime, @path) end
参见 File.utime。更新访问和修改时间。
Source
# File pathname_builtin.rb, line 1057 def world_readable?() File.world_readable?(@path) end
Source
# File pathname_builtin.rb, line 1084 def world_writable?() File.world_writable?(@path) end
Source
# File pathname_builtin.rb, line 1081 def writable?() FileTest.writable?(@path) end
Source
# File pathname_builtin.rb, line 1087 def writable_real?() FileTest.writable_real?(@path) end
Source
# File pathname_builtin.rb, line 890 def write(...) File.write(@path, ...) end
将 contents 写入文件。参见 File.write。
Source
# File pathname_builtin.rb, line 1090 def zero?() FileTest.zero?(@path) end
参见 FileTest.zero?。