class Pathname
pathname.rb¶↑
Object-OrientedPathname Class
- Author
Tanaka Akira <akr@m17n.org>
- Documentation
Author and Gavin Sinclair
For documentation, see classPathname.
Pathname represents the name of a file or directory on the filesystem, but not the file itself.
The pathname depends on the Operating System: Unix, Windows, etc. This library works with pathnames of local OS, however non-Unix pathnames are supported experimentally.
APathname 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.
The goal 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 fromFile,FileTest, and some fromDir andFileUtils is included, in an unsurprising way. It is essentially a facade for all of these, and more.
Examples¶↑
Example 1: UsingPathname¶↑
require'pathname'pn =Pathname.new("/usr/bin/ruby")size =pn.size# 27662isdir =pn.directory?# falsedir =pn.dirname# Pathname:/usr/binbase =pn.basename# Pathname:rubydir,base =pn.split# [Pathname:/usr/bin, Pathname:ruby]data =pn.readpn.open {|f|_ }pn.each_line {|line|_ }
Example 2: Using standard Ruby¶↑
pn ="/usr/bin/ruby"size =File.size(pn)# 27662isdir =File.directory?(pn)# falsedir =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|_ }
Example 3: Special features¶↑
p1 =Pathname.new("/usr/lib")# Pathname:/usr/libp2 =p1+"ruby/1.8"# Pathname:/usr/lib/ruby/1.8p3 =p1.parent# Pathname:/usrp4 =p2.relative_path_from(p3)# Pathname:lib/ruby/1.8pwd =Pathname.pwd# Pathname:/home/gavinpwd.absolute?# truep5 =Pathname.new"."# Pathname:.p5 =p5+"music/../articles"# Pathname:music/../articlesp5.cleanpath# Pathname:articlesp5.realpath# Pathname:/home/gavin/articlesp5.children# [Pathname:/home/gavin/articles/linux, ...]
Breakdown of functionality¶↑
Core methods¶↑
These methods are effectively manipulating aString, because that’s all a path is. None of these access the file system except formountpoint?,children,each_child,realdirpath andrealpath.
+
File status predicate methods¶↑
These methods are a facade for FileTest:
File property and manipulation methods¶↑
These methods are a facade for File:
Directory methods¶↑
These methods are a facade for Dir:
each_entry(&block)
Utilities¶↑
These methods are a mixture ofFind,FileUtils, and others:
Method documentation¶↑
As the above section shows, most of the methods inPathname are facades. The documentation for these methods generally just says, for instance, “SeeFileTest.writable?”, as you should be familiar with the original method anyway, and its documentation (e.g. throughri) will contain more information. In some cases, a brief description will follow.
Constants
Public Class Methods
Source
# File pathname_builtin.rb, line 1124defPathname.getwd()self.new(Dir.getwd)end
SeeDir.getwd. Returns the current working directory as aPathname.
Source
Source
# File lib/pathname.rb, line 63defself.mktmpdirrequire'tmpdir'unlessdefined?(Dir.mktmpdir)ifblock_given?Dir.mktmpdirdo|dir|dir =self.new(dir)yielddirendelseself.new(Dir.mktmpdir)endend
Creates a tmp directory and wraps the returned path in aPathname object.
Note that you need to require ‘pathname’ to use this method.
SeeDir.mktmpdir
Source
# File pathname_builtin.rb, line 217definitialize(path)unlessString===pathpath =path.to_pathifpath.respond_to?:to_pathpath =path.to_strifpath.respond_to?:to_strraiseTypeError,"Pathname.new requires a String, #to_path or #to_str"unlessString===pathendifpath.include?("\0")raiseArgumentError,"pathname contains \\0: #{path.inspect}"end@path =path.dupend
Create aPathname object from the givenString (or String-like object). Ifpath contains a NUL character (\0), anArgumentError is raised.
Public Instance Methods
Source
# File pathname_builtin.rb, line 669def+(other)other =Pathname.new(other)unlessPathname===otherPathname.new(plus(@path,other.path))end
Appends a pathname fragment toself to produce a newPathname object. Sinceother is considered as a path relative toself, ifother is an absolute path, the newPathname object is created from justother.
p1 =Pathname.new("/usr")# Pathname:/usrp2 =p1+"bin/ruby"# Pathname:/usr/bin/rubyp3 =p1+"/etc/passwd"# Pathname:/etc/passwd# / is aliased to +.p4 =p1/"bin/ruby"# Pathname:/usr/bin/rubyp5 =p1/"/etc/passwd"# Pathname:/etc/passwd
This method doesn’t access the file system; it is pure string manipulation.
Source
static VALUEpath_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);}Provides a case-sensitive comparison operator for pathnames.
Pathname.new('/usr')<=>Pathname.new('/usr/bin')#=> -1Pathname.new('/usr/bin')<=>Pathname.new('/usr/bin')#=> 0Pathname.new('/usr/bin')<=>Pathname.new('/USR/BIN')#=> 1
It will return-1,0 or1 depending on the value of the left argument relative to the right argument. Or it will returnnil if the arguments are not comparable.
Source
# File pathname_builtin.rb, line 242def==(other)returnfalseunlessPathname===otherother.path==@pathend
Compare this pathname withother. The comparison is string-based. Be aware that two different paths (foo.txt and./foo.txt) can refer to the same file.
Source
# File pathname_builtin.rb, line 546defabsolute?ABSOLUTE_PATH.match?@pathend
Predicate method for testing whether a path is absolute.
It returnstrue if the pathname begins with a slash.
p =Pathname.new('/im/sure')p.absolute?#=> truep =Pathname.new('not/so/sure')p.absolute?#=> false
Source
# File pathname_builtin.rb, line 643defascendreturnto_enum(__method__)unlessblock_given?path =@pathyieldselfwhiler =chop_basename(path)path, =rbreakifpath.empty?yieldself.class.new(del_trailing_separator(path))endend
Iterates over and yields a newPathname object for each element in the given path in ascending order.
Pathname.new('/path/to/some/file.rb').ascend {|v|pv}#<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|pv}#<Pathname:path/to/some/file.rb>#<Pathname:path/to/some>#<Pathname:path/to>#<Pathname:path>
Returns anEnumerator 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.
Source
# File pathname_builtin.rb, line 902defatime()File.atime(@path)end
SeeFile.atime. Returns last access time.
Source
# File pathname_builtin.rb, line 976defbasename(...)self.class.new(File.basename(@path,...))end
SeeFile.basename. Returns the last component of the path.
Source
# File pathname_builtin.rb, line 885defbinread(...)File.binread(@path,...)end
SeeFile.binread. Returns all the bytes from the file, or the firstN if specified.
Source
# File pathname_builtin.rb, line 899defbinwrite(...)File.binwrite(@path,...)end
Writescontents to the file, opening it in binary mode.
SeeFile.binwrite.
Source
# File pathname_builtin.rb, line 908defbirthtime()File.birthtime(@path)end
Returns the birth time for the file. If the platform doesn’t have birthtime, raisesNotImplementedError.
SeeFile.birthtime.
Source
# File pathname_builtin.rb, line 1014defblockdev?()FileTest.blockdev?(@path)end
Source
# File pathname_builtin.rb, line 1017defchardev?()FileTest.chardev?(@path)end
Source
# File pathname_builtin.rb, line 762defchildren(with_directory=true)with_directory =falseif@path=='.'result = []Dir.foreach(@path) {|e|nextife=='.'||e=='..'ifwith_directoryresult<<self.class.new(File.join(@path,e))elseresult<<self.class.new(e)end }resultend
Returns the children of the directory (files and subdirectories, not recursive) as an array ofPathname objects.
By default, the returned pathnames will have enough information to access the files. If you setwith_directory tofalse, 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.
Source
# File pathname_builtin.rb, line 917defchmod(mode)File.chmod(mode,@path)end
SeeFile.chmod. Changes permissions.
Source
# File pathname_builtin.rb, line 923defchown(owner,group)File.chown(owner,group,@path)end
SeeFile.chown. Change owner and group of file.
Source
# File pathname_builtin.rb, line 407defcleanpath(consider_symlink=false)ifconsider_symlinkcleanpath_conservativeelsecleanpath_aggressiveendend
Returns clean pathname ofself with consecutive slashes and useless dots removed. The filesystem is not accessed.
Ifconsider_symlink istrue, 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.
Source
# File pathname_builtin.rb, line 911defctime()File.ctime(@path)end
SeeFile.ctime. Returns last (directory entry, not file) change time.
Source
# File pathname_builtin.rb, line 610defdescendreturnto_enum(__method__)unlessblock_given?vs = []ascend {|v|vs<<v }vs.reverse_each {|v|yieldv }nilend
Iterates over and yields a newPathname object for each element in the given path in descending order.
Pathname.new('/path/to/some/file.rb').descend {|v|pv}#<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|pv}#<Pathname:path>#<Pathname:path/to>#<Pathname:path/to/some>#<Pathname:path/to/some/file.rb>
Returns anEnumerator 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.
Source
# File pathname_builtin.rb, line 1043defdirectory?()FileTest.directory?(@path)end
Source
# File pathname_builtin.rb, line 979defdirname()self.class.new(File.dirname(@path))end
SeeFile.dirname. Returns all but the last component of the path.
Source
# File pathname_builtin.rb, line 812defeach_child(with_directory=true,&b)children(with_directory).each(&b)end
Iterates over the children of the directory (files and subdirectories, not recursive).
It yieldsPathname object for each child.
By default, the yielded pathnames will have enough information to access the files.
If you setwith_directory tofalse, then the returned pathnames will contain the filename only.
Pathname("/usr/local").each_child {|f|pf }#=> #<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|pf }#=> #<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.
Source
# File pathname_builtin.rb, line 1137defeach_entry(&block)# :yield: pathnamereturnto_enum(__method__)unlessblock_given?Dir.foreach(@path) {|f|yieldself.class.new(f) }end
Iterates over the entries (files and subdirectories) in the directory. It yields aPathname object for each entry.
This method has existed since 1.8.1.
Source
# File pathname_builtin.rb, line 578defeach_filename# :yield: filenamereturnto_enum(__method__)unlessblock_given?_,names =split_names(@path)names.each {|filename|yieldfilename }nilend
Iterates over each component of the path.
Pathname.new("/usr/bin/ruby").each_filename {|filename| ... } # yields "usr", "bin", and "ruby".Returns anEnumerator if no block was given.
enum = Pathname.new("/usr/bin/ruby").each_filename # ... do stuff ...enum.each { |e| ... } # yields "usr", "bin", and "ruby".Source
Source
# File pathname_builtin.rb, line 1022defempty?ifFileTest.directory?(@path)Dir.empty?(@path)elseFile.empty?(@path)endend
Tests the file is empty.
See Dir#empty? andFileTest.empty?.
Source
# File pathname_builtin.rb, line 1131defentries()Dir.entries(@path).map {|f|self.class.new(f) }end
Return the entries (files and subdirectories) in the directory, each as aPathname object.
Source
# File pathname_builtin.rb, line 1031defexecutable?()FileTest.executable?(@path)end
Source
# File pathname_builtin.rb, line 1034defexecutable_real?()FileTest.executable_real?(@path)end
Source
# File pathname_builtin.rb, line 1037defexist?()FileTest.exist?(@path)end
SeeFileTest.exist?.
Source
# File pathname_builtin.rb, line 985defexpand_path(...)self.class.new(File.expand_path(@path,...))end
SeeFile.expand_path.
Source
# File pathname_builtin.rb, line 982defextname()File.extname(@path)end
SeeFile.extname. Returns the file’s extension.
Source
# File pathname_builtin.rb, line 1046deffile?()FileTest.file?(@path)end
SeeFileTest.file?.
Source
# File lib/pathname.rb, line 30deffind(ignore_error:true)# :yield: pathnamereturnto_enum(__method__,ignore_error:ignore_error)unlessblock_given?require'find'if@path=='.'Find.find(@path,ignore_error:ignore_error) {|f|yieldself.class.new(f.delete_prefix('./')) }elseFind.find(@path,ignore_error:ignore_error) {|f|yieldself.class.new(f) }endend
Iterates over the directory tree in a depth first manner, yielding aPathname for each file under “this” directory.
Note that you need to require ‘pathname’ to use this method.
Returns anEnumerator if no block is given.
Since it is implemented by the standard library moduleFind,Find.prune can be used to control the traversal.
Ifself is., yielded pathnames begin with a filename in the current directory, not./.
SeeFind.find
Source
# File pathname_builtin.rb, line 930deffnmatch(pattern,...)File.fnmatch(pattern,@path,...)end
SeeFile.fnmatch. Returntrue if the receiver matches the given pattern.
Source
# File pathname_builtin.rb, line 933deffnmatch?(pattern,...)File.fnmatch?(pattern,@path,...)end
SeeFile.fnmatch? (same asfnmatch).
Source
# File pathname_builtin.rb, line 231deffreezesuper@path.freezeselfend
Object#freezeSource
# File pathname_builtin.rb, line 937defftype()File.ftype(@path)end
SeeFile.ftype. Returns “type” of file (“file”, “directory”, etc).
Source
# File pathname_builtin.rb, line 1115defglob(*args,**kwargs)# :yield: pathnameifblock_given?Dir.glob(*args,**kwargs,base:@path) {|f|yieldself+f }elseDir.glob(*args,**kwargs,base:@path).map {|f|self+f }endend
Returns or yieldsPathname objects.
Pathname("ruby-2.4.2").glob("R*.md")#=> [#<Pathname:ruby-2.4.2/README.md>, #<Pathname:ruby-2.4.2/README.ja.md>]
SeeDir.glob. This method uses thebase keyword argument ofDir.glob.
Source
# File pathname_builtin.rb, line 1040defgrpowned?()FileTest.grpowned?(@path)end
Source
# File pathname_builtin.rb, line 729defjoin(*args)returnselfifargs.empty?result =args.popresult =Pathname.new(result)unlessPathname===resultreturnresultifresult.absolute?args.reverse_each {|arg|arg =Pathname.new(arg)unlessPathname===argresult =arg+resultreturnresultifresult.absolute? }self+resultend
Joins the given pathnames ontoself to create a newPathname object. This is effectively the same as usingPathname#+ to appendself and all arguments sequentially.
path0 =Pathname.new("/usr")# Pathname:/usrpath0 =path0.join("bin/ruby")# Pathname:/usr/bin/ruby# is the same aspath1 =Pathname.new("/usr")+"bin/ruby"# Pathname:/usr/bin/rubypath0==path1#=> true
Source
# File pathname_builtin.rb, line 920deflchmod(mode)File.lchmod(mode,@path)end
SeeFile.lchmod.
Source
# File pathname_builtin.rb, line 926deflchown(owner,group)File.lchown(owner,group,@path)end
SeeFile.lchown.
Source
# File pathname_builtin.rb, line 973deflutime(atime,mtime)File.lutime(atime,mtime,@path)end
Update the access and modification times of the file.
Same asPathname#utime, but does not follow symbolic links.
SeeFile.lutime.
Source
# File pathname_builtin.rb, line 940defmake_link(old)File.link(old,@path)end
SeeFile.link. Creates a hard link.
Source
# File pathname_builtin.rb, line 960defmake_symlink(old)File.symlink(old,@path)end
SeeFile.symlink. Creates a symbolic link.
Source
# File pathname_builtin.rb, line 1143defmkdir(...)Dir.mkdir(@path,...)end
SeeDir.mkdir. Create the referenced directory.
Source
# File pathname_builtin.rb, line 338defmkpath(mode:nil)path =@path=='/'?@path:@path.chomp('/')stack = []untilFile.directory?(path)||File.dirname(path)==pathstack.pushpathpath =File.dirname(path)endstack.reverse_eachdo|dir|dir =dir=='/'?dir:dir.chomp('/')ifmodeDir.mkdirdir,modeFile.chmodmode,direlseDir.mkdirdirendrescueSystemCallErrorraiseunlessFile.directory?(dir)endselfend
Creates a full path, including any intermediate directories that don’t yet exist.
Source
# File pathname_builtin.rb, line 514defmountpoint?beginstat1 =self.lstatstat2 =self.parent.lstatstat1.dev!=stat2.dev||stat1.ino==stat2.inorescueErrno::ENOENTfalseendend
Returnstrue ifself points to a mountpoint.
Source
# File pathname_builtin.rb, line 914defmtime()File.mtime(@path)end
SeeFile.mtime. Returns last modification time.
Source
# File pathname_builtin.rb, line 943defopen(...)# :yield: fileFile.open(@path,...)end
SeeFile.open. Opens the file for reading or writing.
Source
# File pathname_builtin.rb, line 1149defopendir(&block)# :yield: dirDir.open(@path,&block)end
SeeDir.open.
Source
# File pathname_builtin.rb, line 1055defowned?()FileTest.owned?(@path)end
SeeFileTest.owned?.
Source
# File pathname_builtin.rb, line 509defparentself+'..'end
Returns the parent directory.
This is same asself + '..'.
Source
# File pathname_builtin.rb, line 1049defpipe?()FileTest.pipe?(@path)end
SeeFileTest.pipe?.
Source
# File pathname_builtin.rb, line 881defread(...)File.read(@path,...)end
SeeFile.read. Returns all data from the file, or the firstN bytes if specified.
Source
# File pathname_builtin.rb, line 1058defreadable?()FileTest.readable?(@path)end
Source
# File pathname_builtin.rb, line 1064defreadable_real?()FileTest.readable_real?(@path)end
Source
# File pathname_builtin.rb, line 888defreadlines(...)File.readlines(@path,...)end
SeeFile.readlines. Returns all the lines from the file.
Source
# File pathname_builtin.rb, line 948defreadlink()self.class.new(File.readlink(@path))end
SeeFile.readlink. Read symbolic link.
Source
# File pathname_builtin.rb, line 1007defrealdirpath(...)self.class.new(File.realdirpath(@path,...))end
Returns the real (absolute) pathname ofself in the actual filesystem.
Does not contain symlinks or useless dots,.. and..
The last component of the real pathname can be nonexistent.
Source
# File pathname_builtin.rb, line 1000defrealpath(...)self.class.new(File.realpath(@path,...))end
Returns the real (absolute) pathname forself in the actual filesystem.
Does not contain symlinks or useless dots,.. and..
All components of the pathname must exist when this method is called.
Source
# File pathname_builtin.rb, line 561defrelative?!absolute?end
The opposite ofPathname#absolute?
It returnsfalse if the pathname begins with a slash.
p =Pathname.new('/im/sure')p.relative?#=> falsep =Pathname.new('not/so/sure')p.relative?#=> true
Source
# File pathname_builtin.rb, line 830defrelative_path_from(base_directory)base_directory =Pathname.new(base_directory)unlessbase_directory.is_a?Pathnamedest_directory =self.cleanpath.pathbase_directory =base_directory.cleanpath.pathdest_prefix =dest_directorydest_names = []whiler =chop_basename(dest_prefix)dest_prefix,basename =rdest_names.unshiftbasenameifbasename!='.'endbase_prefix =base_directorybase_names = []whiler =chop_basename(base_prefix)base_prefix,basename =rbase_names.unshiftbasenameifbasename!='.'endunlessSAME_PATHS[dest_prefix,base_prefix]raiseArgumentError,"different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"endwhile!dest_names.empty?&&!base_names.empty?&&SAME_PATHS[dest_names.first,base_names.first]dest_names.shiftbase_names.shiftendifbase_names.include?'..'raiseArgumentError,"base_directory has ..: #{base_directory.inspect}"endbase_names.fill('..')relpath_names =base_names+dest_namesifrelpath_names.empty?Pathname.new('.')elsePathname.new(File.join(*relpath_names))endend
Returns a relative path from the givenbase_directory to the receiver.
Ifself is absolute, thenbase_directory must be absolute too.
Ifself is relative, thenbase_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.
Source
# File pathname_builtin.rb, line 951defrename(to)File.rename(@path,to)end
SeeFile.rename. Rename the file.
Source
# File pathname_builtin.rb, line 1146defrmdir()Dir.rmdir(@path)end
SeeDir.rmdir. Remove the referenced directory.
Source
# File lib/pathname.rb, line 48defrmtree(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)selfend
Recursively deletes a directory, including all directories beneath it.
Note that you need to require ‘pathname’ to use this method.
Source
# File pathname_builtin.rb, line 531defroot?chop_basename(@path)==nil&&SEPARATOR_PAT.match?(@path)end
Predicate method for root directories. Returnstrue if the pathname consists of consecutive slashes.
It doesn’t access the filesystem. So it may returnfalse for some pathnames which points to roots such as/usr/...
Source
# File pathname_builtin.rb, line 1070defsetgid?()FileTest.setgid?(@path)end
SeeFileTest.setgid?.
Source
# File pathname_builtin.rb, line 1067defsetuid?()FileTest.setuid?(@path)end
SeeFileTest.setuid?.
Source
# File pathname_builtin.rb, line 1076defsize?()FileTest.size?(@path)end
SeeFileTest.size?.
Source
# File pathname_builtin.rb, line 1052defsocket?()FileTest.socket?(@path)end
SeeFileTest.socket?.
Source
# File pathname_builtin.rb, line 989defsplit()array =File.split(@path)raiseTypeError,'wrong argument type nil (expected Array)'unlessArray===arrayarray.map {|f|self.class.new(f) }end
SeeFile.split. Returns thedirname and thebasename in anArray.
Source
# File pathname_builtin.rb, line 954defstat()File.stat(@path)end
SeeFile.stat. Returns aFile::Stat object.
Source
# File pathname_builtin.rb, line 1079defsticky?()FileTest.sticky?(@path)end
SeeFileTest.sticky?.
Source
static VALUEpath_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));}Return a pathname which is substituted byString#sub.
path1 =Pathname.new('/usr/bin/perl')path1.sub('perl','ruby')#=> #<Pathname:/usr/bin/ruby>
Source
# File pathname_builtin.rb, line 300defsub_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)..]endself.class.new(@path.chomp(ext)+repl)end
Return a pathname withrepl 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>
Source
# File pathname_builtin.rb, line 1082defsymlink?()FileTest.symlink?(@path)end
Source
# File pathname_builtin.rb, line 891defsysopen(...)File.sysopen(@path,...)end
SeeFile.sysopen.
Source
# File pathname_builtin.rb, line 262defto_s@path.dupend
Return the path as aString.
Source
# File pathname_builtin.rb, line 963deftruncate(length)File.truncate(@path,length)end
SeeFile.truncate. Truncate the file tolength bytes.
Source
# File pathname_builtin.rb, line 1157defunlink()Dir.unlink@pathrescueErrno::ENOTDIRFile.unlink@pathend
Removes a file or directory, usingFile.unlink orDir.unlink as necessary.
Source
# File pathname_builtin.rb, line 966defutime(atime,mtime)File.utime(atime,mtime,@path)end
SeeFile.utime. Update the access and modification times.
Source
# File pathname_builtin.rb, line 1061defworld_readable?()File.world_readable?(@path)end
Source
# File pathname_builtin.rb, line 1088defworld_writable?()File.world_writable?(@path)end
Source
# File pathname_builtin.rb, line 1085defwritable?()FileTest.writable?(@path)end
Source
# File pathname_builtin.rb, line 1091defwritable_real?()FileTest.writable_real?(@path)end
Source
# File pathname_builtin.rb, line 894defwrite(...)File.write(@path,...)end
Writescontents to the file. SeeFile.write.
Source
# File pathname_builtin.rb, line 1094defzero?()FileTest.zero?(@path)end
SeeFileTest.zero?.