Path::Name represents a pathname which locates a file in a filesystem. It supports only Unix style pathnames. It does not represent the file itself. A Path::Name 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.
Path::Name is immutable. It has no method for destructive update.
The value of this class is to manipulate file path information in a neater way than standard Ruby provides. The examples below demonstrate the difference. All functionality from File, FileTest, and some from Dir and FileUtils is included, in an unsurprising way. It is essentially a facade for all of these, and more.
require 'pathname' p = Path::Name.new("/usr/bin/ruby") size = p.size # 27662 isdir = p.directory? # false dir = p.dirname # Path::Name:/usr/bin base = p.basename # Path::Name:ruby dir, base = p.split # [Path::Name:/usr/bin, Path::Name:ruby] data = p.read p.open { |f| _ } p.each_line { |line| _ }
p = "/usr/bin/ruby" size = File.size(p) # 27662 isdir = File.directory?(p) # false dir = File.dirname(p) # "/usr/bin" base = File.basename(p) # "ruby" dir, base = File.split(p) # ["/usr/bin", "ruby"] data = File.read(p) File.open(p) { |f| _ } File.foreach(p) { |line| _ }
p1 = Path::Name.new("/usr/lib") # Path::Name:/usr/lib p2 = p1 + "ruby/1.8" # Path::Name:/usr/lib/ruby/1.8 p3 = p1.parent # Path::Name:/usr p4 = p2.relative_path_from(p3) # Path::Name:lib/ruby/1.8 pwd = Path::Name.pwd # Path::Name:/home/gavin pwd.absolute? # true p5 = Path::Name.new "." # Path::Name:. p5 = p5 + "music/../articles" # Path::Name:music/../articles p5.cleanpath # Path::Name:articles p5.realpath # Path::Name:/home/gavin/articles p5.children # [Path::Name:/home/gavin/articles/linux, ...]
These methods are effectively manipulating a String, because that’s all a path is. Except for mountpoint?, children, and realpath, they don’t access the filesystem.
These methods are a facade for FileTest:
These methods are a facade for File:
These methods are a facade for Dir:
These methods are a facade for IO:
These methods are a mixture of Find, FileUtils, and others:
As the above section shows, most of the methods in Path::Name are facades. The documentation for these methods generally just says, for instance, “See FileTest.writable?“, as you should be familiar with the original method anyway, and its documentation (e.g. through ri) will contain more information. In some cases, a brief description will follow.
Active path separator.
p1 = Pathname.new('/') p2 = p1 / 'usr' / 'share' #=> Pathname:/usr/share
# File lib/path/name.rb, line 1094 def self./(path) #/ new(path) end
(Not documented)
# File lib/path/name.rb, line 201 def [](path) new(path) end
Alternate to Pathname#new.
Pathname['/usr/share']
# File lib/path/name.rb, line 1085 def self.[](path) new(path) end
(Not documented)
# File lib/path/name.rb, line 193 def create( path=[], abs=false, trail=false ) o = self.allocate o.instance_variable_set("@path", path.dup) o.instance_variable_set("@absolute", abs ? true : false) o.instance_variable_set("@trail", trail ? true : false) o end
See Dir.getwd. Returns the current working directory as a Path::Name.
# File lib/path/name.rb, line 924 def self.getwd() Path::Name.new(Dir.getwd) end
See Dir.glob. Returns or yields Path::Name objects.
# File lib/path/name.rb, line 915 def self.glob(*args) # :yield: p if block_given? Dir.glob(*args) {|f| yield Path::Name.new(f) } else Dir.glob(*args).map {|f| Path::Name.new(f) } end end
Home constant for building paths from root directory onward.
TODO: Pathname#home needs to be more robust.
# File lib/path/name.rb, line 1107 def self.home self.class.new('~') end
Create a Path::Name object from the given String (or String-like object). If path contains a NUL character (0), an ArgumentError is raised.
# File lib/path/name.rb, line 209 def initialize(*path) path = path.join('/') @absolute = path[0,1] == '/' ? true : false @trail = (path.size > 1 and path[-1,1] == '/') ? true : false @path = path.split(%r{/}) @path.delete('') #case path #when Path::Name # @path = path.pathlist.dup # @absolute = path.absolute? #when Array # @path = path.collect{|e| e.to_str} # @absolute = absolute #else # path = path.to_str if path.respond_to?(:to_str) # raise ArgumentError, "pathname contains \\0: #{@path.inspect}" if /\0/ =~ @path # @path = path.split(%r{/}) # @path.unshift('') if path[0,1] == '/' # absolute path #end self.taint if path.tainted? end
Platform dependent null device.
# File lib/path/name.rb, line 1119 def self.null case RUBY_PLATFORM when /mswin/i 'NUL' when /amiga/i 'NIL:' when /openvms/i 'NL:' else '/dev/null' end end
Path::Name#+ appends a pathname fragment to this one to produce a new Path::Name object.
p1 = Path::Name.new("/usr") # Path::Name:/usr p2 = p1 + "bin/ruby" # Path::Name:/usr/bin/ruby p3 = p1 + "/etc/passwd" # Path::Name:/etc/passwd
This method doesn’t access the file system; it is pure string manipulation.
# File lib/path/name.rb, line 534 def +(other) path = self.class.new(File.join(to_s, other.to_s)) path.cleanpath #other = Path::Name.new(other) unless Path::Name === other #return other if other.absolute? #pth = (@path + other.pathlist) #pth.delete('.') #Path::Name.create(pth, @absolute, other.trail?) # path1 = @path# # path2 = other.to_s # while m2 = %r{\A\.\.(?:/+|\z)}.match(path2) and # m1 = %r{(\A|/+)([^/]+)\z}.match(path1) and # %r{\A(?:\.|\.\.)\z} !~ m1[2] # path1 = m1[1].empty? ? '.' : '/' if (path1 = m1.pre_match).empty? # path2 = '.' if (path2 = m2.post_match).empty? # end # if %r{\A/+\z} =~ path1 # while m2 = %r{\A\.\.(?:/+|\z)}.match(path2) # path2 = '.' if (path2 = m2.post_match).empty? # end # end # # return Path::Name.new(path2) if path1 == '.' # return Path::Name.new(path1) if path2 == '.' # # if %r{/\z} =~ path1 # Path::Name.new(path1 + path2) # else # Path::Name.new(path1 + '/' + path2) # end end
append directory or file name to path
# File lib/path/name.rb, line 312 def <<(name) name = name.to_str @path << name unless name.strip.empty? @path end
Provides for comparing pathnames, case-sensitively.
# File lib/path/name.rb, line 269 def <=>(other) return nil unless Path::Name === other return 1 if absolute? and not other.absolute? return -1 if other.absolute? and not absolute? r = @path <=> other.pathlist return r unless r == 0 return 1 if trail? and not other.trail? return -1 if other.trail? and not trail? 0 end
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.
# File lib/path/name.rb, line 258 def ==(other) return false unless Path::Name === other if @absolute return false unless other.absolute? end @path == other.pathlist end
# File lib/path/name.rb, line 243 def [](*globs) Path::List.new(self)[*globs] end
Predicate method for testing whether a path is absolute. It returns true if the pathname begins with a slash.
# File lib/path/name.rb, line 499 def absolute? #%r{\A/} =~ @path ? true : false @absolute end
Calls the block for every successive parent directory of the directory path until the root (absolute path) or . (relative path) is reached.
# File lib/path/name.rb, line 688 def ascend(inclusive=false,&block) # :yield: cur_dir = self yield( cur_dir.cleanpath ) if inclusive until cur_dir.root? or cur_dir == self.class.new(".") cur_dir = cur_dir.parent yield cur_dir end end
See File.atime. Returns last access time.
# File lib/path/name.rb, line 748 def atime() File.atime(path) end
See File.basename. Returns the last component of the path.
# File lib/path/name.rb, line 809 def basename(*args) Path::Name.new(File.basename(path, *args)) end
See FileTest.blockdev?.
# File lib/path/name.rb, line 843 def blockdev?() FileTest.blockdev?(path) end
See FileTest.chardev?.
# File lib/path/name.rb, line 846 def chardev?() FileTest.chardev?(path) end
Path::Name#chdir is obsoleted at 1.8.1.
# File lib/path/name.rb, line 928 def chdir(&block) warn "Path::Name#chdir is obsoleted. Use Dir.chdir." Dir.chdir(path, &block) end
Returns the children of the directory (files and subdirectories, not recursive) as an array of Path::Name 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:
p = Path::Name("/usr/lib/ruby/1.8") p.children # -> [ Path::Name:/usr/lib/ruby/1.8/English.rb, Path::Name:/usr/lib/ruby/1.8/Env.rb, Path::Name:/usr/lib/ruby/1.8/abbrev.rb, ... ] p.children(false) # -> [ Path::Name:English.rb, Path::Name:Env.rb, Path::Name:abbrev.rb, ... ]
Note that the result never contain the entries . and .. in the directory because they are not children.
This method has existed since 1.8.1.
# File lib/path/name.rb, line 614 def children(with_directory=true) with_directory = false if @path == ['.'] result = [] Dir.foreach(to_s) {|e| next if e == '.' || e == '..' if with_directory result << Path::Name.create(@path + [e], absolute?) else result << Path::Name.new(e) end } result end
See File.chmod. Changes permissions.
# File lib/path/name.rb, line 757 def chmod(mode) File.chmod(mode, path) end
See File.chown. Change owner and group of file.
# File lib/path/name.rb, line 763 def chown(owner, group) File.chown(owner, group, path) end
Path::Name#chroot is obsoleted at 1.8.1.
# File lib/path/name.rb, line 934 def chroot warn "Path::Name#chroot is obsoleted. Use Dir.chroot." Dir.chroot(path) end
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 realpath.
# File lib/path/name.rb, line 327 def cleanpath(consider_symlink=false) if consider_symlink cleanpath_conservative else cleanpath_aggressive end end
See File.ctime. Returns last (directory entry, not file) change time.
# File lib/path/name.rb, line 751 def ctime() File.ctime(path) end
(Not documented)
# File lib/path/name.rb, line 492 def current? #%r{\A/+\z} =~ @path ? true : false (!@absolute) and @path.empty? end
Calls the block for every successive subdirectory of the directory path from the root (absolute path) until . (relative path) is reached.
# File lib/path/name.rb, line 700 def descend() @path.scan(%r{[^/]*/?})[0...-1].inject('') do |path, dir| yield self.class.new(path << dir) path end end
Like directory? but return self if true, otherwise nil.
# File lib/path/name.rb, line 864 def dir? ; directory? ? self : nil ; end
Path::Name#dir_foreach is obsoleted at 1.8.1.
# File lib/path/name.rb, line 952 def dir_foreach(*args, &block) warn "Path::Name#dir_foreach is obsoleted. Use Path::Name#each_entry." each_entry(*args, &block) end
See FileTest.directory?.
# File lib/path/name.rb, line 861 def directory?() FileTest.directory?(path) end
See File.dirname. Returns all but the last component of the path.
# File lib/path/name.rb, line 812 def dirname() Path::Name.new(File.dirname(path)) end
Iterates over the entries (files and subdirectories) in the directory. It yields a Path::Name object for each entry.
This method has existed since 1.8.1.
# File lib/path/name.rb, line 947 def each_entry(&block) # :yield: p Dir.foreach(path) {|f| yield Path::Name.new(f) } end
Iterates over each component of the path.
Path::Name.new("/usr/bin/ruby").each_filename {|filename| ... } # yields "usr", "bin", and "ruby".
# File lib/path/name.rb, line 519 def each_filename # :yield: e #@path.scan(%r{[^/]+}) { yield $& } @path.each { |e| yield e } end
# File lib/path/name.rb, line 996 def empty? Dir.glob(::File.join(self.to_s, '*')).empty? end
Return the entries (files and subdirectories) in the directory, each as a Path::Name object.
# File lib/path/name.rb, line 941 def entries() Dir.entries(path).map {|f| Path::Name.new(f) } end
See FileTest.executable?.
# File lib/path/name.rb, line 849 def executable?() FileTest.executable?(path) end
See FileTest.executable_real?.
# File lib/path/name.rb, line 852 def executable_real?() FileTest.executable_real?(path) end
See FileTest.exist?.
# File lib/path/name.rb, line 855 def exist?() FileTest.exist?(path) end
See File.expand_path.
# File lib/path/name.rb, line 818 def expand_path(*args) Path::Name.new(File.expand_path(path, *args)) end
See File.extname. Returns the file’s extension.
# File lib/path/name.rb, line 815 def extname() File.extname(path) end
See FileTest.file?.
# File lib/path/name.rb, line 867 def file?() FileTest.file?(path) end
Path::Name#find is an iterator to traverse a directory tree in a depth first manner. It yields a Path::Name for each file under “this” directory.
Since it is implemented by find.rb, Find.prune can be used to control the traverse.
If self is ., yielded pathnames begin with a filename in the current directory, not ./.
# File lib/path/name.rb, line 1013 def find(&block) # :yield: p require 'find' if @path == ['.'] Find.find(path) {|f| yield Path::Name.new(f.sub(%r{\A\./}, '')) } else Find.find(path) {|f| yield Path::Name.new(f) } end end
See File.fnmatch. Return true if the receiver matches the given pattern.
# File lib/path/name.rb, line 770 def fnmatch(pattern, *args) File.fnmatch(pattern, path, *args) end
See File.fnmatch? (same as fnmatch).
# File lib/path/name.rb, line 773 def fnmatch?(pattern, *args) File.fnmatch?(pattern, path, *args) end
This method is obsoleted at 1.8.1. Use each_line or each_entry.
# File lib/path/name.rb, line 1067 def foreach(*args, &block) warn "Path::Name#foreach is obsoleted. Use each_line or each_entry." if FileTest.directory? path # For polymorphism between Dir.foreach and IO.foreach, # Path::Name#foreach doesn't yield Path::Name object. Dir.foreach(path, *args, &block) else IO.foreach(path, *args, &block) end end
Path::Name#foreachline is obsoleted at 1.8.1. Use each_line.
# File lib/path/name.rb, line 728 def foreachline(*args, &block) warn "Path::Name#foreachline is obsoleted. Use Path::Name#each_line." each_line(*args, &block) end
(Not documented)
# File lib/path/name.rb, line 233 def freeze() super ; @path.freeze ; self end
See File.ftype. Returns “type” of file (“file”, “directory”, etc).
# File lib/path/name.rb, line 777 def ftype() File.ftype(path) end
# File lib/path/name.rb, line 969 def glob(match, *opts) flags = 0 opts.each do |opt| case opt when Symbol, String flags += ::File.const_get("FNM_#{opt}".upcase) else flags += opt end end Dir.glob(::File.join(self.to_s, match), flags).collect{ |m| self.class.new(m) } end
# File lib/path/name.rb, line 982 def glob_first(match, *opts) flags = 0 opts.each do |opt| case opt when Symbol, String flags += ::File.const_get("FNM_#{opt}".upcase) else flags += opt end end file = ::Dir.glob(::File.join(self.to_s, match), flags).first file ? self.class.new(file) : nil end
See FileTest.grpowned?.
# File lib/path/name.rb, line 858 def grpowned?() FileTest.grpowned?(path) end
Path::Name#join joins pathnames.
path0.join(path1, ..., pathN) is the same as path0 + path1 + ... + pathN.
HELP ME!!!
# File lib/path/name.rb, line 579 def join(*args) Path::Name.new(*args) #args.unshift self #result = args.pop #result = Path::Name.new(result) unless Path::Name === result #return result if result.absolute? #args.reverse_each {|arg| # arg = Path::Name.new(arg) unless Path::Name === arg # result = arg + result # return result if result.absolute? #} #result end
See File.lchmod.
# File lib/path/name.rb, line 760 def lchmod(mode) File.lchmod(mode, path) end
See File.lchown.
# File lib/path/name.rb, line 766 def lchown(owner, group) File.lchown(owner, group, path) end
Path::Name#link is confusing and obsoleted because the receiver/argument order is inverted to corresponding system call.
# File lib/path/name.rb, line 826 def link(old) warn 'Path::Name#link is obsoleted. Use Path::Name#make_link.' File.link(old, path) end
See File.lstat.
# File lib/path/name.rb, line 797 def lstat() File.lstat(path) end
See File.link. Creates a hard link.
# File lib/path/name.rb, line 780 def make_link(old) File.link(old, path) end
See File.symlink. Creates a symbolic link.
# File lib/path/name.rb, line 800 def make_symlink(old) File.symlink(old, path) end
See Dir.mkdir. Create the referenced directory.
# File lib/path/name.rb, line 958 def mkdir(*args) Dir.mkdir(path, *args) end
See FileUtils.mkpath. Creates a full path, including any intermediate directories that don’t yet exist.
# File lib/path/name.rb, line 1027 def mkpath require 'fileutils' FileUtils.mkpath(path) nil end
mountpoint? returns true if self points to a mountpoint.
# File lib/path/name.rb, line 469 def mountpoint? begin stat1 = self.lstat stat2 = self.parent.lstat stat1.dev == stat2.dev && stat1.ino == stat2.ino || stat1.dev != stat2.dev rescue Errno::ENOENT false end end
See File.mtime. Returns last modification time.
# File lib/path/name.rb, line 754 def mtime() File.mtime(path) end
See File.open. Opens the file for reading or writing.
# File lib/path/name.rb, line 783 def open(*args, &block) # :yield: file File.open(path, *args, &block) end
See Dir.open.
# File lib/path/name.rb, line 964 def opendir(&block) # :yield: dir Dir.open(path, &block) end
# File lib/path/name.rb, line 1048 def outofdate?(*sources) ! uptodate?(*sources) end
See FileTest.owned?.
# File lib/path/name.rb, line 876 def owned?() FileTest.owned?(path) end
parent returns the parent directory.
This is same as self + '..'.
# File lib/path/name.rb, line 464 def parent self << '..' end
Stores the array of the componet parts of the pathname.
protected
# File lib/path/name.rb, line 250 def pathlist @path end
See FileTest.pipe?.
# File lib/path/name.rb, line 870 def pipe?() FileTest.pipe?(path) end
See IO.read. Returns all the bytes from the file, or the first N if specified.
# File lib/path/name.rb, line 735 def read(*args) IO.read(path, *args) end
See FileTest.readable?.
# File lib/path/name.rb, line 879 def readable?() FileTest.readable?(path) end
See FileTest.readable_real?.
# File lib/path/name.rb, line 882 def readable_real?() FileTest.readable_real?(path) end
See IO.readlines. Returns all the lines from the file.
# File lib/path/name.rb, line 738 def readlines(*args) IO.readlines(path, *args) end
See File.readlink. Read symbolic link.
# File lib/path/name.rb, line 788 def readlink() Path::Name.new(File.readlink(path)) end
Returns a real (absolute) pathname of self in the actual filesystem. The real pathname doesn’t contain symlinks or useless dots.
No arguments should be given; the old behaviour is obsoleted.
# File lib/path/name.rb, line 396 def realpath(*args) unless args.empty? warn "The argument for Path::Name#realpath is obsoleted." end force_absolute = args.fetch(0, true) if @path.first == '' top = '/' unresolved = @path.slice(1..-1) #.scan(%r{[^/]+}) elsif force_absolute # Although POSIX getcwd returns a pathname which contains no symlink, # 4.4BSD-Lite2 derived getcwd may return the environment variable $PWD # which may contain a symlink. # So the return value of Dir.pwd should be examined. top = '/' unresolved = Dir.pwd.split(%r{/}) + @path else top = '' unresolved = @path.dup #.scan(%r{[^/]+}) end resolved = [] until unresolved.empty? case unresolved.last when '.' unresolved.pop when '..' resolved.unshift unresolved.pop else loop_check = {} while (stat = File.lstat(path = top + unresolved.join('/'))).symlink? symlink_id = "#{stat.dev}:#{stat.ino}" raise Errno::ELOOP.new(path) if loop_check[symlink_id] loop_check[symlink_id] = true if %r{\A/} =~ (link = File.readlink(path)) top = '/' unresolved = link.split(%r{/}) #scan(%r{[^/]+}) else unresolved[-1,1] = link.split(%r{/}) #.scan(%r{[^/]+}) end end next if (filename = unresolved.pop) == '.' if filename != '..' && resolved.first == '..' resolved.shift else resolved.unshift filename end end end if top == '/' resolved.shift while resolved[0] == '..' end if resolved.empty? Path::Name.new(top.empty? ? '.' : '/') else if top.empty? Path::Name.new(resolved) else Path::Name.new(resolved, true) end end end
The opposite of absolute?
# File lib/path/name.rb, line 505 def relative? !@absolute end
relative_path_from returns a relative path from the argument to the receiver. If self is absolute, the argument must be absolute too. If self is relative, the argument must be relative too.
relative_path_from doesn’t access the filesystem. It assumes no symlinks.
ArgumentError is raised when it cannot find a relative path.
This method has existed since 1.8.1.
# File lib/path/name.rb, line 639 def relative_path_from(base_directory) if self.absolute? != base_directory.absolute? raise ArgumentError, "relative path between absolute and relative path: #{self.inspect}, #{base_directory.inspect}" end dest = [] self.cleanpath.each_filename {|f| next if f == '.' dest << f } base = [] base_directory.cleanpath.each_filename {|f| next if f == '.' base << f } while !base.empty? && !dest.empty? && base[0] == dest[0] base.shift dest.shift end if base.include? '..' raise ArgumentError, "base_directory has ..: #{base_directory.inspect}" end base.fill '..' relpath = base + dest if relpath.empty? Path::Name.new('.') else Path::Name.create(relpath, false) #.join('/')) end end
See File.rename. Rename the file.
# File lib/path/name.rb, line 791 def rename(to) File.rename(path, to) end
See Dir.rmdir. Remove the referenced directory.
# File lib/path/name.rb, line 961 def rmdir() Dir.rmdir(path) end
See FileUtils.rm_r. Deletes a directory and all beneath it.
# File lib/path/name.rb, line 1034 def rmtree # The name "rmtree" is borrowed from File::Path of Perl. # File::Path provides "mkpath" and "rmtree". require 'fileutils' FileUtils.rm_r(path) nil end
root? is a predicate for root directories. I.e. it returns true if the pathname consists of consecutive slashes.
It doesn’t access actual filesystem. So it may return false for some pathnames which points to roots such as /usr/...
# File lib/path/name.rb, line 487 def root? #%r{\A/+\z} =~ @path ? true : false @absolute and @path.empty? end
# File lib/path/name.rb, line 676 def rootname # this should be fairly robust path_re = Regexp.new('[' + Regexp.escape(File::Separator + %q{\/}) + ']') head, tail = path.split(path_re, 2) return '.' if path == head return '/' if head.empty? self.class.new(head) end
See FileTest.setgid?.
# File lib/path/name.rb, line 888 def setgid?() FileTest.setgid?(path) end
See FileTest.setuid?.
# File lib/path/name.rb, line 885 def setuid?() FileTest.setuid?(path) end
See FileTest.size.
# File lib/path/name.rb, line 891 def size() FileTest.size(path) end
See FileTest.size?.
# File lib/path/name.rb, line 894 def size?() FileTest.size?(path) end
See FileTest.socket?.
# File lib/path/name.rb, line 873 def socket?() FileTest.socket?(path) end
# File lib/path/name.rb, line 708 def split_root path_re = Regexp.new('[' + Regexp.escape(File::Separator + %q{\/}) + ']') head, tail = *path.split(path_re, 2) [self.class.new(head), self.class.new(tail)] end
See File.stat. Returns a File::Stat object.
# File lib/path/name.rb, line 794 def stat() File.stat(path) end
See FileTest.sticky?.
# File lib/path/name.rb, line 897 def sticky?() FileTest.sticky?(path) end
Path::Name#symlink is confusing and obsoleted because the receiver/argument order is inverted to corresponding system call.
# File lib/path/name.rb, line 833 def symlink(old) warn 'Path::Name#symlink is obsoleted. Use Path::Name#make_symlink.' File.symlink(old, path) end
See FileTest.symlink?.
# File lib/path/name.rb, line 900 def symlink?() FileTest.symlink?(path) end
See IO.sysopen.
# File lib/path/name.rb, line 741 def sysopen(*args) IO.sysopen(path, *args) end
(Not documented)
# File lib/path/name.rb, line 234 def taint() super ; @path.taint ; self end
Convertion method for path names. This returns self.
# File lib/path/name.rb, line 238 def to_path self end
Return the path as a String (same as pathname).
# File lib/path/name.rb, line 295 def to_s s = '' s << '/' if @absolute s << @path.join('/') s << '/' if @trail s << '.' if s.empty? s end
(Not documented)
# File lib/path/name.rb, line 509 def trail? @trail end
See File.truncate. Truncate the file to length bytes.
# File lib/path/name.rb, line 803 def truncate(length) File.truncate(path, length) end
Removes a file or directory, using File.unlink or Dir.unlink as necessary.
# File lib/path/name.rb, line 1057 def unlink() begin Dir.unlink path rescue Errno::ENOTDIR File.unlink path end end
(Not documented)
# File lib/path/name.rb, line 235 def untaint() super ; @path.untaint ; self end
# File lib/path/name.rb, line 1043 def uptodate?(*sources) ::FileUtils.uptodate?(to_s, sources.flatten) end
See File.utime. Update the access and modification times.
# File lib/path/name.rb, line 806 def utime(atime, mtime) File.utime(atime, mtime, path) end
See FileTest.writable?.
# File lib/path/name.rb, line 903 def writable?() FileTest.writable?(path) end
Clean the path simply by resolving and removing excess “.” and “..” entries. Nothing more, nothing less.
# File lib/path/name.rb, line 338 def cleanpath_aggressive # cleanpath_aggressive assumes: # * no symlink # * all pathname prefix contained in the pathname is existing directory return Path::Name.create([],@absolute,@trail) if path.empty? absolute = absolute? trail = trail? names = [] @path.each {|name| next if name == '.' if name == '..' if names.empty? next if absolute else if names.last != '..' names.pop next end end end names << name } return Path::Name.new(absolute ? '/' : '.') if names.empty? #path = [] #path << '' if absolute #path.concat(names) Path::Name.create(names, absolute) #, trail) end
(Not documented)
# File lib/path/name.rb, line 368 def cleanpath_conservative return Path::Name.create([],@absolute) if @path.empty? names = @path.dup #.scan(%r{[^/]+}) last_dot = (names.last == '.') names.delete('.') names.shift while names.first == '..' if absolute? return Path::Name.new(absolute? ? '/' : '.') if names.empty? newpath = [] trail = false #newpath << '' if absolute? newpath.concat(names) if names.last != '..' if last_dot newpath << '.' elsif trail? #%r{/\z} =~ @path #HOW TO DEAL WITH TRAILING '/' ? trail = true end end Path::Name.create(newpath, absolute?, trail) end
Disabled; run with --debug to generate this.