Class: Pathname
- Inherits:
-
Object
- Object
- Pathname
- Defined in:
- lib/pathname.rb,
ext/pathname/pathname.c,
lib/pathname.rb,
lib/pathname.rb,
lib/pathname_builtin.rb,
lib/pathname_builtin.rb,
lib/pathname_builtin.rb,
lib/pathname_builtin.rb,
lib/pathname_builtin.rb,
lib/pathname_builtin.rb
Overview
-
mixed *
Constant Summary collapse
- VERSION =
The version string.
"0.5.0"- SEPARATOR_PAT =
Regexp that matches a separator.
/#{SEPARATOR_LIST}/
Class Method Summary collapse
-
.getwd ⇒ Object
(also: pwd)
call-seq: Pathname.getwd -> new_pathname.
-
.glob(*args, **kwargs) ⇒ Object
See
Dir.glob. -
.mktmpdir ⇒ Object
Creates a tmp directory and wraps the returned path in a Pathname object.
Instance Method Summary collapse
-
#+(other) ⇒ Object
(also: #/)
call-seq: self + other -> new_pathname.
-
#<=>(other) ⇒ Object
Provides for comparing pathnames, case-sensitively.
-
#==(other) ⇒ Object
(also: #===, #eql?)
Compare this pathname with
other. -
#absolute? ⇒ Boolean
Predicate method for testing whether a path is absolute.
-
#ascend {|_self| ... } ⇒ Object
Iterates over and yields a new Pathname object for each element in the given path in ascending order.
-
#atime ⇒ Object
See
File.atime. -
#basename ⇒ Object
See
File.basename. -
#binread ⇒ Object
See
File.binread. -
#binwrite ⇒ Object
Writes
contentsto the file, opening it in binary mode. -
#birthtime ⇒ Object
Returns the birth time for the file.
-
#blockdev? ⇒ Boolean
See
FileTest.blockdev?. -
#chardev? ⇒ Boolean
See
FileTest.chardev?. -
#children(with_directory = true) ⇒ Object
Returns the children of the directory (files and subdirectories, not recursive) as an array of Pathname objects.
-
#chmod(mode) ⇒ Object
See
File.chmod. -
#chown(owner, group) ⇒ Object
See
File.chown. -
#cleanpath(consider_symlink = false) ⇒ Object
Returns clean pathname of
selfwith consecutive slashes and useless dots removed. -
#ctime ⇒ Object
See
File.ctime. -
#descend ⇒ Object
Iterates over and yields a new Pathname object for each element in the given path in descending order.
-
#directory? ⇒ Boolean
See
FileTest.directory?. -
#dirname ⇒ Object
See
File.dirname. -
#each_child(with_directory = true, &b) ⇒ Object
Iterates over the children of the directory (files and subdirectories, not recursive).
-
#each_entry(&block) ⇒ Object
Iterates over the entries (files and subdirectories) in the directory.
-
#each_filename ⇒ Object
Iterates over each component of the path.
-
#each_line ⇒ Object
#each_line iterates over the line in the file.
-
#empty? ⇒ Boolean
Tests the file is empty.
-
#entries ⇒ Object
Return the entries (files and subdirectories) in the directory, each as a Pathname object.
-
#executable? ⇒ Boolean
See
FileTest.executable?. -
#executable_real? ⇒ Boolean
See
FileTest.executable_real?. -
#exist? ⇒ Boolean
See
FileTest.exist?. -
#expand_path ⇒ Object
See
File.expand_path. -
#extname ⇒ Object
See
File.extname. -
#file? ⇒ Boolean
See
FileTest.file?. -
#find(ignore_error: true) ⇒ Object
Iterates over the directory tree in a depth first manner, yielding a Pathname for each file under “this” directory.
-
#fnmatch(pattern) ⇒ Object
See
File.fnmatch. -
#fnmatch?(pattern) ⇒ Boolean
See
File.fnmatch?(same as #fnmatch). -
#freeze ⇒ Object
Freze self.
-
#ftype ⇒ Object
See
File.ftype. -
#glob(*args, **kwargs) ⇒ Object
Returns or yields Pathname objects.
-
#grpowned? ⇒ Boolean
See
FileTest.grpowned?. -
#hash ⇒ Object
:nodoc:.
-
#initialize(path) ⇒ Pathname
constructor
Create a Pathname object from the given String (or String-like object).
-
#inspect ⇒ Object
:nodoc:.
-
#join(*args) ⇒ Object
Joins the given pathnames onto
selfto create a new Pathname object. -
#lchmod(mode) ⇒ Object
See
File.lchmod. -
#lchown(owner, group) ⇒ Object
See
File.lchown. -
#lstat ⇒ Object
See
File.lstat. -
#lutime(atime, mtime) ⇒ Object
Update the access and modification times of the file.
-
#make_link(old) ⇒ Object
See
File.link. -
#make_symlink(old) ⇒ Object
See
File.symlink. -
#mkdir ⇒ Object
See
Dir.mkdir. -
#mkpath(mode: nil) ⇒ Object
Creates a full path, including any intermediate directories that don’t yet exist.
-
#mountpoint? ⇒ Boolean
Returns
trueifselfpoints to a mountpoint. -
#mtime ⇒ Object
See
File.mtime. -
#open ⇒ Object
See
File.open. -
#opendir(&block) ⇒ Object
See
Dir.open. -
#owned? ⇒ Boolean
See
FileTest.owned?. -
#parent ⇒ Object
Returns the parent directory.
-
#pipe? ⇒ Boolean
See
FileTest.pipe?. -
#read ⇒ Object
See
File.read. -
#readable? ⇒ Boolean
See
FileTest.readable?. -
#readable_real? ⇒ Boolean
See
FileTest.readable_real?. -
#readlines ⇒ Object
See
File.readlines. -
#readlink ⇒ Object
See
File.readlink. -
#realdirpath ⇒ Object
Returns the real (absolute) pathname of
selfin the actual filesystem. -
#realpath ⇒ Object
Returns the real (absolute) pathname for
selfin the actual filesystem. -
#relative? ⇒ Boolean
The opposite of Pathname#absolute?.
-
#relative_path_from(base_directory) ⇒ Object
Returns a relative path from the given
base_directoryto the receiver. -
#rename(to) ⇒ Object
See
File.rename. -
#rmdir ⇒ Object
See
Dir.rmdir. -
#rmtree(noop: nil, verbose: nil, secure: nil) ⇒ Object
Recursively deletes a directory, including all directories beneath it.
-
#root? ⇒ Boolean
Predicate method for root directories.
-
#setgid? ⇒ Boolean
See
FileTest.setgid?. -
#setuid? ⇒ Boolean
See
FileTest.setuid?. -
#size ⇒ Object
See
FileTest.size. -
#size? ⇒ Boolean
See
FileTest.size?. -
#socket? ⇒ Boolean
See
FileTest.socket?. -
#split ⇒ Object
See
File.split. -
#stat ⇒ Object
See
File.stat. -
#sticky? ⇒ Boolean
See
FileTest.sticky?. -
#sub(pattern, *args, **kwargs, &block) ⇒ Object
Return a pathname which is substituted by String#sub.
-
#sub_ext(repl) ⇒ Object
Return a pathname with
repladded as a suffix to the basename. -
#symlink? ⇒ Boolean
See
FileTest.symlink?. -
#sysopen ⇒ Object
See
File.sysopen. -
#to_s ⇒ Object
(also: #to_path)
Return the path as a String.
-
#truncate(length) ⇒ Object
See
File.truncate. -
#unlink ⇒ Object
(also: #delete)
Removes a file or directory, using
File.unlinkorDir.unlinkas necessary. -
#utime(atime, mtime) ⇒ Object
See
File.utime. -
#world_readable? ⇒ Boolean
See
FileTest.world_readable?. -
#world_writable? ⇒ Boolean
See
FileTest.world_writable?. -
#writable? ⇒ Boolean
See
FileTest.writable?. -
#writable_real? ⇒ Boolean
See
FileTest.writable_real?. -
#write ⇒ Object
Writes
contentsto the file. -
#zero? ⇒ Boolean
See
FileTest.zero?.
Constructor Details
#initialize(path) ⇒ Pathname
Create a Pathname object from the given String (or String-like object). If path contains a NUL character (\0), an ArgumentError is raised.
214 215 216 217 218 |
# File 'lib/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 |
Class Method Details
.getwd ⇒ Object Also known as: pwd
call-seq:
Pathname.getwd -> new_pathname
Returns a new Pathname object containing the path to the current working directory (equivalent to Pathname.new(Dir.getwd)):
Pathname.getwd # => #<Pathname:/home>
1167 |
# File 'lib/pathname_builtin.rb', line 1167 def Pathname.getwd() self.new(Dir.getwd) end |
.glob(*args, **kwargs) ⇒ Object
See Dir.glob. Returns or yields Pathname objects.
1136 1137 1138 1139 1140 1141 1142 |
# File 'lib/pathname_builtin.rb', line 1136 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 |
.mktmpdir ⇒ Object
Creates a tmp directory and wraps the returned path in a Pathname object.
See Dir.mktmpdir
63 64 65 66 67 68 69 70 71 72 73 |
# 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 |
Instance Method Details
#+(other) ⇒ Object Also known as: /
call-seq:
self + other -> new_pathname
Returns a new Pathname object; argument other may be a string or another pathname.
When other specifies a relative path (see #relative?), it is combined with self to form a new pathname:
Pathname.new('/a/b') + 'c' # => #<Pathname:/a/b/c>
Extra component separators ('/') are removed:
Pathname.new('/a/b/') + 'c' # => #<Pathname:/a/b/c>
Extra current-directory components ('.') are removed:
Pathname.new('a') + '.' # => #<Pathname:a>
Pathname.new('.') + 'a' # => #<Pathname:a>
Pathname.new('.') + '.' # => #<Pathname:.>
Parent-directory components ('..') are:
-
Resolved, when possible:
Pathname.new('a') + '..' # => #<Pathname:.> Pathname.new('a/b') + '..' # => #<Pathname:a> Pathname.new('/') + '../a' # => #<Pathname:/a> Pathname.new('a') + '../b' # => #<Pathname:b> Pathname.new('a/b') + '../c' # => #<Pathname:a/c> Pathname.new('a//b/c') + '../d//e' # => #<Pathname:a//b/d//e> -
Removed, when not needed:
Pathname.new('/') + '..' # => #<Pathname:/> -
Retained, when needed:
Pathname.new('..') + '..' # => #<Pathname:../..> Pathname.new('..') + '../a' # => #<Pathname:../../a>
When other specifies an absolute path (see #absolute?), equivalent to Pathname.new(other.to_s):
Pathname.new('/a') + '/b/c' # => #<Pathname:/b/c>
Occurrences of '/', '.', and '..' are preserved:
Pathname.new('/a') + '//b//c/./../d' # => #<Pathname://b//c/./../d>
This method does not access the file system, so other need not represent an existing (or even a valid) file or directory path:
Pathname.new('/var') + 'nosuch:ever' # => #<Pathname:/var/nosuch:ever>
705 706 707 708 |
# File 'lib/pathname_builtin.rb', line 705 def +(other) other = Pathname.new(other) unless Pathname === other Pathname.new(plus(@path, other.path)) end |
#<=>(other) ⇒ Object
Provides for comparing pathnames, case-sensitively.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'ext/pathname/pathname.c', line 31
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);
}
|
#==(other) ⇒ Object Also known as: ===, eql?
Compare this pathname with other. The comparison is string-based. Be aware that two different paths (foo.txt and ./foo.txt) can refer to the same file.
234 235 236 237 |
# File 'lib/pathname_builtin.rb', line 234 def ==(other) return false unless Pathname === other other.path == @path end |
#absolute? ⇒ Boolean
542 543 544 |
# File 'lib/pathname_builtin.rb', line 542 def absolute? ABSOLUTE_PATH.match? @path end |
#ascend {|_self| ... } ⇒ Object
Iterates over and yields a new Pathname object for each element in the given path in ascending order.
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>
Returns an Enumerator if no block was given.
enum = Pathname.new("/usr/bin/ruby").ascend
# ... do stuff ...
enum.each { |e| ... }
# yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
It doesn’t access the filesystem.
639 640 641 642 643 644 645 646 647 648 |
# File 'lib/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 |
#atime ⇒ Object
See File.atime. Returns last access time.
938 |
# File 'lib/pathname_builtin.rb', line 938 def atime() File.atime(@path) end |
#basename ⇒ Object
See File.basename. Returns the last component of the path.
1012 |
# File 'lib/pathname_builtin.rb', line 1012 def basename(...) self.class.new(File.basename(@path, ...)) end |
#binread ⇒ Object
See File.binread. Returns all the bytes from the file, or the first N if specified.
921 |
# File 'lib/pathname_builtin.rb', line 921 def binread(...) File.binread(@path, ...) end |
#binwrite ⇒ Object
Writes contents to the file, opening it in binary mode.
See File.binwrite.
935 |
# File 'lib/pathname_builtin.rb', line 935 def binwrite(...) File.binwrite(@path, ...) end |
#birthtime ⇒ Object
Returns the birth time for the file. If the platform doesn’t have birthtime, raises NotImplementedError.
See File.birthtime.
944 |
# File 'lib/pathname_builtin.rb', line 944 def birthtime() File.birthtime(@path) end |
#blockdev? ⇒ Boolean
See FileTest.blockdev?.
1050 |
# File 'lib/pathname_builtin.rb', line 1050 def blockdev?() FileTest.blockdev?(@path) end |
#chardev? ⇒ Boolean
See FileTest.chardev?.
1053 |
# File 'lib/pathname_builtin.rb', line 1053 def chardev?() FileTest.chardev?(@path) end |
#children(with_directory = true) ⇒ Object
Returns the children of the directory (files and subdirectories, not recursive) as an array of Pathname objects.
By default, the returned pathnames will have enough information to access the files. If you set with_directory to false, then the returned pathnames will contain the filename only.
For example:
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, ... ]
Note that the results never contain the entries . and .. in the directory because they are not children.
798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/pathname_builtin.rb', line 798 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 |
#chmod(mode) ⇒ Object
See File.chmod. Changes permissions.
953 |
# File 'lib/pathname_builtin.rb', line 953 def chmod(mode) File.chmod(mode, @path) end |
#chown(owner, group) ⇒ Object
See File.chown. Change owner and group of file.
959 |
# File 'lib/pathname_builtin.rb', line 959 def chown(owner, group) File.chown(owner, group, @path) end |
#cleanpath(consider_symlink = false) ⇒ Object
Returns clean pathname of self with consecutive slashes and useless dots removed. The filesystem is not accessed.
If consider_symlink is true, then a more conservative algorithm is used to avoid breaking symbolic linkages. This may retain more .. entries than absolutely necessary, but without accessing the filesystem, this can’t be avoided.
See Pathname#realpath.
403 404 405 406 407 408 409 |
# File 'lib/pathname_builtin.rb', line 403 def cleanpath(consider_symlink=false) if consider_symlink cleanpath_conservative else cleanpath_aggressive end end |
#ctime ⇒ Object
See File.ctime. Returns last (directory entry, not file) change time.
947 |
# File 'lib/pathname_builtin.rb', line 947 def ctime() File.ctime(@path) end |
#descend ⇒ Object
Iterates over and yields a new Pathname object for each element in the given path in descending order.
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>
Returns an Enumerator if no block was given.
enum = Pathname.new("/usr/bin/ruby").descend
# ... do stuff ...
enum.each { |e| ... }
# yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
It doesn’t access the filesystem.
606 607 608 609 610 611 612 |
# File 'lib/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 |
#directory? ⇒ Boolean
See FileTest.directory?.
1079 |
# File 'lib/pathname_builtin.rb', line 1079 def directory?() FileTest.directory?(@path) end |
#dirname ⇒ Object
See File.dirname. Returns all but the last component of the path.
1015 |
# File 'lib/pathname_builtin.rb', line 1015 def dirname() self.class.new(File.dirname(@path)) end |
#each_child(with_directory = true, &b) ⇒ Object
Iterates over the children of the directory (files and subdirectories, not recursive).
It yields Pathname object for each child.
By default, the yielded pathnames will have enough information to access the files.
If you set with_directory to false, then the returned pathnames will contain the filename only.
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>
Note that the results never contain the entries . and .. in the directory because they are not children.
See Pathname#children
848 849 850 |
# File 'lib/pathname_builtin.rb', line 848 def each_child(with_directory=true, &b) children(with_directory).each(&b) end |
#each_entry(&block) ⇒ Object
Iterates over the entries (files and subdirectories) in the directory. It yields a Pathname object for each entry.
This method has existed since 1.8.1.
1180 1181 1182 1183 |
# File 'lib/pathname_builtin.rb', line 1180 def each_entry(&block) # :yield: pathname return to_enum(__method__) unless block_given? Dir.foreach(@path) {|f| yield self.class.new(f) } end |
#each_filename ⇒ Object
Iterates over each component of the path.
Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
# yields "usr", "bin", and "ruby".
Returns an Enumerator if no block was given.
enum = Pathname.new("/usr/bin/ruby").each_filename
# ... do stuff ...
enum.each { |e| ... }
# yields "usr", "bin", and "ruby".
574 575 576 577 578 579 |
# File 'lib/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 |
#each_line ⇒ Object
#each_line iterates over the line in the file. It yields a String object for each line.
This method has existed since 1.8.1.
911 912 913 |
# File 'lib/pathname_builtin.rb', line 911 def each_line(...) # :yield: line File.foreach(@path, ...) end |
#empty? ⇒ Boolean
Tests the file is empty.
See Dir#empty? and FileTest.empty?.
1058 1059 1060 1061 1062 1063 1064 |
# File 'lib/pathname_builtin.rb', line 1058 def empty? if FileTest.directory?(@path) Dir.empty?(@path) else File.empty?(@path) end end |
#entries ⇒ Object
Return the entries (files and subdirectories) in the directory, each as a Pathname object.
1174 |
# File 'lib/pathname_builtin.rb', line 1174 def entries() Dir.entries(@path).map {|f| self.class.new(f) } end |
#executable? ⇒ Boolean
See FileTest.executable?.
1067 |
# File 'lib/pathname_builtin.rb', line 1067 def executable?() FileTest.executable?(@path) end |
#executable_real? ⇒ Boolean
See FileTest.executable_real?.
1070 |
# File 'lib/pathname_builtin.rb', line 1070 def executable_real?() FileTest.executable_real?(@path) end |
#exist? ⇒ Boolean
See FileTest.exist?.
1073 |
# File 'lib/pathname_builtin.rb', line 1073 def exist?() FileTest.exist?(@path) end |
#expand_path ⇒ Object
See File.expand_path.
1021 |
# File 'lib/pathname_builtin.rb', line 1021 def (...) self.class.new(File.(@path, ...)) end |
#extname ⇒ Object
See File.extname. Returns the file’s extension.
1018 |
# File 'lib/pathname_builtin.rb', line 1018 def extname() File.extname(@path) end |
#file? ⇒ Boolean
See FileTest.file?.
1082 |
# File 'lib/pathname_builtin.rb', line 1082 def file?() FileTest.file?(@path) end |
#find(ignore_error: true) ⇒ Object
Iterates over the directory tree in a depth first manner, yielding a Pathname for each file under “this” directory.
Returns an Enumerator if no block is given.
Since it is implemented by the standard library module Find, Find.prune can be used to control the traversal.
If self is ., yielded pathnames begin with a filename in the current directory, not ./.
See Find.find
34 35 36 37 38 39 40 41 42 |
# File 'lib/pathname.rb', line 34 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 |
#fnmatch(pattern) ⇒ Object
See File.fnmatch. Return true if the receiver matches the given pattern.
966 |
# File 'lib/pathname_builtin.rb', line 966 def fnmatch(pattern, ...) File.fnmatch(pattern, @path, ...) end |
#fnmatch?(pattern) ⇒ Boolean
See File.fnmatch? (same as #fnmatch).
969 |
# File 'lib/pathname_builtin.rb', line 969 def fnmatch?(pattern, ...) File.fnmatch?(pattern, @path, ...) end |
#freeze ⇒ Object
Freze self.
223 224 225 226 227 |
# File 'lib/pathname_builtin.rb', line 223 def freeze super @path.freeze self end |
#ftype ⇒ Object
See File.ftype. Returns “type” of file (“file”, “directory”, etc).
973 |
# File 'lib/pathname_builtin.rb', line 973 def ftype() File.ftype(@path) end |
#glob(*args, **kwargs) ⇒ Object
1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/pathname_builtin.rb', line 1151 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 |
#grpowned? ⇒ Boolean
See FileTest.grpowned?.
1076 |
# File 'lib/pathname_builtin.rb', line 1076 def grpowned?() FileTest.grpowned?(@path) end |
#hash ⇒ Object
:nodoc:
249 250 251 |
# File 'lib/pathname_builtin.rb', line 249 def hash # :nodoc: @path.hash end |
#inspect ⇒ Object
:nodoc:
261 262 263 |
# File 'lib/pathname_builtin.rb', line 261 def inspect # :nodoc: "#<#{self.class}:#{@path}>" end |
#join(*args) ⇒ Object
Joins the given pathnames onto self to create a new Pathname object. This is effectively the same as using Pathname#+ to append self and all arguments sequentially.
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
765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/pathname_builtin.rb', line 765 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 |
#lchmod(mode) ⇒ Object
See File.lchmod.
956 |
# File 'lib/pathname_builtin.rb', line 956 def lchmod(mode) File.lchmod(mode, @path) end |
#lchown(owner, group) ⇒ Object
See File.lchown.
962 |
# File 'lib/pathname_builtin.rb', line 962 def lchown(owner, group) File.lchown(owner, group, @path) end |
#lstat ⇒ Object
See File.lstat.
993 |
# File 'lib/pathname_builtin.rb', line 993 def lstat() File.lstat(@path) end |
#lutime(atime, mtime) ⇒ Object
Update the access and modification times of the file.
Same as Pathname#utime, but does not follow symbolic links.
See File.lutime.
1009 |
# File 'lib/pathname_builtin.rb', line 1009 def lutime(atime, mtime) File.lutime(atime, mtime, @path) end |
#make_link(old) ⇒ Object
See File.link. Creates a hard link.
976 |
# File 'lib/pathname_builtin.rb', line 976 def make_link(old) File.link(old, @path) end |
#make_symlink(old) ⇒ Object
See File.symlink. Creates a symbolic link.
996 |
# File 'lib/pathname_builtin.rb', line 996 def make_symlink(old) File.symlink(old, @path) end |
#mkdir ⇒ Object
See Dir.mkdir. Create the referenced directory.
1186 |
# File 'lib/pathname_builtin.rb', line 1186 def mkdir(...) Dir.mkdir(@path, ...) end |
#mkpath(mode: nil) ⇒ Object
Creates a full path, including any intermediate directories that don’t yet exist.
See FileUtils.mkpath and FileUtils.mkdir_p
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/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 |
#mountpoint? ⇒ Boolean
Returns true if self points to a mountpoint.
510 511 512 513 514 515 516 517 518 |
# File 'lib/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 |
#mtime ⇒ Object
See File.mtime. Returns last modification time.
950 |
# File 'lib/pathname_builtin.rb', line 950 def mtime() File.mtime(@path) end |
#open ⇒ Object
See File.open. Opens the file for reading or writing.
979 980 981 |
# File 'lib/pathname_builtin.rb', line 979 def open(...) # :yield: file File.open(@path, ...) end |
#opendir(&block) ⇒ Object
See Dir.open.
1192 1193 1194 |
# File 'lib/pathname_builtin.rb', line 1192 def opendir(&block) # :yield: dir Dir.open(@path, &block) end |
#owned? ⇒ Boolean
See FileTest.owned?.
1091 |
# File 'lib/pathname_builtin.rb', line 1091 def owned?() FileTest.owned?(@path) end |
#parent ⇒ Object
Returns the parent directory.
This is same as self + '..'.
505 506 507 |
# File 'lib/pathname_builtin.rb', line 505 def parent self + '..' end |
#pipe? ⇒ Boolean
See FileTest.pipe?.
1085 |
# File 'lib/pathname_builtin.rb', line 1085 def pipe?() FileTest.pipe?(@path) end |
#read ⇒ Object
See File.read. Returns all data from the file, or the first N bytes if specified.
917 |
# File 'lib/pathname_builtin.rb', line 917 def read(...) File.read(@path, ...) end |
#readable? ⇒ Boolean
See FileTest.readable?.
1094 |
# File 'lib/pathname_builtin.rb', line 1094 def readable?() FileTest.readable?(@path) end |
#readable_real? ⇒ Boolean
See FileTest.readable_real?.
1100 |
# File 'lib/pathname_builtin.rb', line 1100 def readable_real?() FileTest.readable_real?(@path) end |
#readlines ⇒ Object
See File.readlines. Returns all the lines from the file.
924 |
# File 'lib/pathname_builtin.rb', line 924 def readlines(...) File.readlines(@path, ...) end |
#readlink ⇒ Object
See File.readlink. Read symbolic link.
984 |
# File 'lib/pathname_builtin.rb', line 984 def readlink() self.class.new(File.readlink(@path)) end |
#realdirpath ⇒ Object
Returns the real (absolute) pathname of self in the actual filesystem.
Does not contain symlinks or useless dots, .. and ..
The last component of the real pathname can be nonexistent.
1043 |
# File 'lib/pathname_builtin.rb', line 1043 def realdirpath(...) self.class.new(File.realdirpath(@path, ...)) end |
#realpath ⇒ Object
Returns the real (absolute) pathname for self in the actual filesystem.
Does not contain symlinks or useless dots, .. and ..
All components of the pathname must exist when this method is called.
1036 |
# File 'lib/pathname_builtin.rb', line 1036 def realpath(...) self.class.new(File.realpath(@path, ...)) end |
#relative? ⇒ Boolean
557 558 559 |
# File 'lib/pathname_builtin.rb', line 557 def relative? !absolute? end |
#relative_path_from(base_directory) ⇒ Object
Returns a relative path from the given base_directory to the receiver.
If self is absolute, then base_directory must be absolute too.
If self is relative, then base_directory must be relative too.
This method doesn’t access the filesystem. It assumes no symlinks.
ArgumentError is raised when it cannot find a relative path.
Note that this method does not handle situations where the case sensitivity of the filesystem in use differs from the operating system default.
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
# File 'lib/pathname_builtin.rb', line 866 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 |
#rename(to) ⇒ Object
See File.rename. Rename the file.
987 |
# File 'lib/pathname_builtin.rb', line 987 def rename(to) File.rename(@path, to) end |
#rmdir ⇒ Object
See Dir.rmdir. Remove the referenced directory.
1189 |
# File 'lib/pathname_builtin.rb', line 1189 def rmdir() Dir.rmdir(@path) end |
#rmtree(noop: nil, verbose: nil, secure: nil) ⇒ Object
Recursively deletes a directory, including all directories beneath it.
See FileUtils.rm_rf
50 51 52 53 54 55 56 |
# File 'lib/pathname.rb', line 50 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 |
#root? ⇒ Boolean
Predicate method for root directories. Returns true if the pathname consists of consecutive slashes.
It doesn’t access the filesystem. So it may return false for some pathnames which points to roots such as /usr/...
527 528 529 |
# File 'lib/pathname_builtin.rb', line 527 def root? chop_basename(@path) == nil && SEPARATOR_PAT.match?(@path) end |
#setgid? ⇒ Boolean
See FileTest.setgid?.
1106 |
# File 'lib/pathname_builtin.rb', line 1106 def setgid?() FileTest.setgid?(@path) end |
#setuid? ⇒ Boolean
See FileTest.setuid?.
1103 |
# File 'lib/pathname_builtin.rb', line 1103 def setuid?() FileTest.setuid?(@path) end |
#size ⇒ Object
See FileTest.size.
1109 |
# File 'lib/pathname_builtin.rb', line 1109 def size() FileTest.size(@path) end |
#size? ⇒ Boolean
See FileTest.size?.
1112 |
# File 'lib/pathname_builtin.rb', line 1112 def size?() FileTest.size?(@path) end |
#socket? ⇒ Boolean
See FileTest.socket?.
1088 |
# File 'lib/pathname_builtin.rb', line 1088 def socket?() FileTest.socket?(@path) end |
#split ⇒ Object
See File.split. Returns the #dirname and the #basename in an Array.
1025 1026 1027 1028 1029 |
# File 'lib/pathname_builtin.rb', line 1025 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 |
#stat ⇒ Object
See File.stat. Returns a File::Stat object.
990 |
# File 'lib/pathname_builtin.rb', line 990 def stat() File.stat(@path) end |
#sticky? ⇒ Boolean
See FileTest.sticky?.
1115 |
# File 'lib/pathname_builtin.rb', line 1115 def sticky?() FileTest.sticky?(@path) end |
#sub(pattern, *args, **kwargs, &block) ⇒ Object
Return a pathname which is substituted by String#sub.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'ext/pathname/pathname.c', line 72
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));
}
|
#sub_ext(repl) ⇒ Object
Return a pathname with repl added as a suffix to the basename.
If self has no extension part, repl is appended.
Pathname.new(‘/usr/bin/shutdown’).sub_ext(‘.rb’)
#=> #<Pathname:/usr/bin/shutdown.rb>
292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/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 |
#symlink? ⇒ Boolean
See FileTest.symlink?.
1118 |
# File 'lib/pathname_builtin.rb', line 1118 def symlink?() FileTest.symlink?(@path) end |
#sysopen ⇒ Object
See File.sysopen.
927 |
# File 'lib/pathname_builtin.rb', line 927 def sysopen(...) File.sysopen(@path, ...) end |
#to_s ⇒ Object Also known as: to_path
Return the path as a String.
254 255 256 |
# File 'lib/pathname_builtin.rb', line 254 def to_s @path.dup end |
#truncate(length) ⇒ Object
See File.truncate. Truncate the file to length bytes.
999 |
# File 'lib/pathname_builtin.rb', line 999 def truncate(length) File.truncate(@path, length) end |
#unlink ⇒ Object Also known as: delete
Removes a file or directory, using File.unlink or Dir.unlink as necessary.
1200 1201 1202 1203 1204 |
# File 'lib/pathname_builtin.rb', line 1200 def unlink() Dir.unlink @path rescue Errno::ENOTDIR File.unlink @path end |
#utime(atime, mtime) ⇒ Object
See File.utime. Update the access and modification times.
1002 |
# File 'lib/pathname_builtin.rb', line 1002 def utime(atime, mtime) File.utime(atime, mtime, @path) end |
#world_readable? ⇒ Boolean
See FileTest.world_readable?.
1097 |
# File 'lib/pathname_builtin.rb', line 1097 def world_readable?() File.world_readable?(@path) end |
#world_writable? ⇒ Boolean
See FileTest.world_writable?.
1124 |
# File 'lib/pathname_builtin.rb', line 1124 def world_writable?() File.world_writable?(@path) end |
#writable? ⇒ Boolean
See FileTest.writable?.
1121 |
# File 'lib/pathname_builtin.rb', line 1121 def writable?() FileTest.writable?(@path) end |
#writable_real? ⇒ Boolean
See FileTest.writable_real?.
1127 |
# File 'lib/pathname_builtin.rb', line 1127 def writable_real?() FileTest.writable_real?(@path) end |
#write ⇒ Object
Writes contents to the file. See File.write.
930 |
# File 'lib/pathname_builtin.rb', line 930 def write(...) File.write(@path, ...) end |
#zero? ⇒ Boolean
See FileTest.zero?.
1130 |
# File 'lib/pathname_builtin.rb', line 1130 def zero?() FileTest.zero?(@path) end |