4 # Object-Oriented Pathname Class
6 # Author:: Tanaka Akira <akr@m17n.org>
7 # Documentation:: Author and Gavin Sinclair
9 # For documentation, see class Pathname.
11 # <tt>pathname.rb</tt> is distributed with Ruby since 1.8.0.
17 # Pathname represents a pathname which locates a file in a filesystem.
18 # The pathname depends on OS: Unix, Windows, etc.
19 # Pathname library works with pathnames of local OS.
20 # However non-Unix pathnames are supported experimentally.
22 # It does not represent the file itself.
23 # A Pathname can be relative or absolute. It's not until you try to
24 # reference the file that it even matters whether the file exists or not.
26 # Pathname is immutable. It has no method for destructive update.
28 # The value of this class is to manipulate file path information in a neater
29 # way than standard Ruby provides. The examples below demonstrate the
30 # difference. *All* functionality from File, FileTest, and some from Dir and
31 # FileUtils is included, in an unsurprising way. It is essentially a facade for
32 # all of these, and more.
36 # === Example 1: Using Pathname
39 # p = Pathname.new("/usr/bin/ruby")
40 # size = p.size # 27662
41 # isdir = p.directory? # false
42 # dir = p.dirname # Pathname:/usr/bin
43 # base = p.basename # Pathname:ruby
44 # dir, base = p.split # [Pathname:/usr/bin, Pathname:ruby]
47 # p.each_line { |line| _ }
49 # === Example 2: Using standard Ruby
52 # size = File.size(p) # 27662
53 # isdir = File.directory?(p) # false
54 # dir = File.dirname(p) # "/usr/bin"
55 # base = File.basename(p) # "ruby"
56 # dir, base = File.split(p) # ["/usr/bin", "ruby"]
58 # File.open(p) { |f| _ }
59 # File.foreach(p) { |line| _ }
61 # === Example 3: Special features
63 # p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
64 # p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
65 # p3 = p1.parent # Pathname:/usr
66 # p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
67 # pwd = Pathname.pwd # Pathname:/home/gavin
68 # pwd.absolute? # true
69 # p5 = Pathname.new "." # Pathname:.
70 # p5 = p5 + "music/../articles" # Pathname:music/../articles
71 # p5.cleanpath # Pathname:articles
72 # p5.realpath # Pathname:/home/gavin/articles
73 # p5.children # [Pathname:/home/gavin/articles/linux, ...]
75 # == Breakdown of functionality
79 # These methods are effectively manipulating a String, because that's all a path
80 # is. Except for #mountpoint?, #children, and #realpath, they don't access the
89 # - #relative_path_from
96 # === File status predicate methods
98 # These methods are a facade for FileTest:
103 # - #executable_real?
124 # === File property and manipulation methods
126 # These methods are a facade for File:
132 # - #chown(owner, group)
133 # - #lchown(owner, group)
134 # - #fnmatch(pattern, *args)
135 # - #fnmatch?(pattern, *args)
138 # - #open(*args, &block)
143 # - #make_symlink(old)
144 # - #truncate(length)
145 # - #utime(atime, mtime)
149 # - #expand_path(*args)
152 # === Directory methods
154 # These methods are a facade for Dir:
155 # - Pathname.glob(*args)
156 # - Pathname.getwd / Pathname.pwd
159 # - #each_entry(&block)
165 # These methods are a facade for IO:
166 # - #each_line(*args, &block)
168 # - #readlines(*args)
173 # These methods are a mixture of Find, FileUtils, and others:
177 # - #unlink / #delete
180 # == Method documentation
182 # As the above section shows, most of the methods in Pathname are facades. The
183 # documentation for these methods generally just says, for instance, "See
184 # FileTest.writable?", as you should be familiar with the original method
185 # anyway, and its documentation (e.g. through +ri+) will contain more
186 # information. In some cases, a brief description will follow.
191 if RUBY_VERSION < "1.9"
194 # to_path is implemented so Pathname objects are usable with File.open, etc.
200 # Create a Pathname object from the given String (or String-like object).
201 # If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
204 path = path.__send__(TO_PATH) if path.respond_to? TO_PATH
208 raise ArgumentError, "pathname contains \\0: #{@path.inspect}"
211 self.taint if @path.tainted?
214 def freeze() super; @path.freeze; self end
215 def taint() super; @path.taint; self end
216 def untaint() super; @path.untaint; self end
219 # Compare this pathname with +other+. The comparison is string-based.
220 # Be aware that two different paths (<tt>foo.txt</tt> and <tt>./foo.txt</tt>)
221 # can refer to the same file.
224 return false unless Pathname === other
230 # Provides for comparing pathnames, case-sensitively.
232 return nil unless Pathname === other
233 @path.tr('/', "\0") <=> other.to_s.tr('/', "\0")
240 # Return the path as a String.
245 # to_path is implemented so Pathname objects are usable with File.open, etc.
246 alias_method TO_PATH, :to_s
248 def inspect # :nodoc:
249 "#<#{self.class}:#{@path}>"
252 # Return a pathname which is substituted by String#sub.
253 def sub(pattern, *rest, &block)
254 self.class.new(@path.sub(pattern, *rest, &block))
257 if File::ALT_SEPARATOR
258 SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/
260 SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
263 # chop_basename(path) -> [pre-basename, basename] or nil
264 def chop_basename(path)
265 base = File.basename(path)
266 if /\A#{SEPARATOR_PAT}?\z/ =~ base
269 return path[0, path.rindex(base)], base
272 private :chop_basename
274 # split_names(path) -> prefix, [name, ...]
275 def split_names(path)
277 while r = chop_basename(path)
279 names.unshift basename
285 def prepend_prefix(prefix, relpath)
288 elsif /#{SEPARATOR_PAT}/ =~ prefix
289 prefix = File.dirname(prefix)
290 prefix = File.join(prefix, "") if File.basename(prefix + 'a') != 'a'
296 private :prepend_prefix
298 # Returns clean pathname of +self+ with consecutive slashes and useless dots
299 # removed. The filesystem is not accessed.
301 # If +consider_symlink+ is +true+, then a more conservative algorithm is used
302 # to avoid breaking symbolic linkages. This may retain more <tt>..</tt>
303 # entries than absolutely necessary, but without accessing the filesystem,
304 # this can't be avoided. See #realpath.
306 def cleanpath(consider_symlink=false)
308 cleanpath_conservative
315 # Clean the path simply by resolving and removing excess "." and ".." entries.
316 # Nothing more, nothing less.
318 def cleanpath_aggressive
322 while r = chop_basename(pre)
336 if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
337 names.shift while names[0] == '..'
339 self.class.new(prepend_prefix(pre, File.join(*names)))
341 private :cleanpath_aggressive
343 # has_trailing_separator?(path) -> bool
344 def has_trailing_separator?(path)
345 if r = chop_basename(path)
347 pre.length + basename.length < path.length
352 private :has_trailing_separator?
354 # add_trailing_separator(path) -> path
355 def add_trailing_separator(path)
356 if File.basename(path + 'a') == 'a'
359 File.join(path, "") # xxx: Is File.join is appropriate to add separator?
362 private :add_trailing_separator
364 def del_trailing_separator(path)
365 if r = chop_basename(path)
368 elsif /#{SEPARATOR_PAT}+\z/o =~ path
369 $` + File.dirname(path)[/#{SEPARATOR_PAT}*\z/o]
374 private :del_trailing_separator
376 def cleanpath_conservative
380 while r = chop_basename(pre)
382 names.unshift base if base != '.'
384 if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
385 names.shift while names[0] == '..'
388 self.class.new(File.dirname(pre))
390 if names.last != '..' && File.basename(path) == '.'
393 result = prepend_prefix(pre, File.join(*names))
394 if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
395 self.class.new(add_trailing_separator(result))
397 self.class.new(result)
401 private :cleanpath_conservative
403 def realpath_rec(prefix, unresolved, h)
405 until unresolved.empty?
412 path = prepend_prefix(prefix, File.join(*(resolved + [n])))
414 if h[path] == :resolving
415 raise Errno::ELOOP.new(path)
417 prefix, *resolved = h[path]
423 link_prefix, link_names = split_names(File.readlink(path))
425 prefix, *resolved = h[path] = realpath_rec(prefix, resolved + link_names, h)
427 prefix, *resolved = h[path] = realpath_rec(link_prefix, link_names, h)
431 h[path] = [prefix, *resolved]
436 return prefix, *resolved
438 private :realpath_rec
441 # Returns a real (absolute) pathname of +self+ in the actual filesystem.
442 # The real pathname doesn't contain symlinks or useless dots.
444 # No arguments should be given; the old behaviour is *obsoleted*.
448 prefix, names = split_names(path)
450 prefix, names2 = split_names(Dir.pwd)
451 names = names2 + names
453 prefix, *names = realpath_rec(prefix, names, {})
454 self.class.new(prepend_prefix(prefix, File.join(*names)))
457 # #parent returns the parent directory.
459 # This is same as <tt>self + '..'</tt>.
464 # #mountpoint? returns +true+ if <tt>self</tt> points to a mountpoint.
468 stat2 = self.parent.lstat
469 stat1.dev == stat2.dev && stat1.ino == stat2.ino ||
470 stat1.dev != stat2.dev
477 # #root? is a predicate for root directories. I.e. it returns +true+ if the
478 # pathname consists of consecutive slashes.
480 # It doesn't access actual filesystem. So it may return +false+ for some
481 # pathnames which points to roots such as <tt>/usr/..</tt>.
484 !!(chop_basename(@path) == nil && /#{SEPARATOR_PAT}/o =~ @path)
487 # Predicate method for testing whether a path is absolute.
488 # It returns +true+ if the pathname begins with a slash.
493 # The opposite of #absolute?
496 while r = chop_basename(path)
503 # Iterates over each component of the path.
505 # Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
506 # # yields "usr", "bin", and "ruby".
508 def each_filename # :yield: filename
509 prefix, names = split_names(@path)
510 names.each {|filename| yield filename }
514 # Iterates over and yields a new Pathname object
515 # for each element in the given path in descending order.
517 # Pathname.new('/path/to/some/file.rb').descend {|v| p v}
520 # #<Pathname:/path/to>
521 # #<Pathname:/path/to/some>
522 # #<Pathname:/path/to/some/file.rb>
524 # Pathname.new('path/to/some/file.rb').descend {|v| p v}
526 # #<Pathname:path/to>
527 # #<Pathname:path/to/some>
528 # #<Pathname:path/to/some/file.rb>
530 # It doesn't access actual filesystem.
532 # This method is available since 1.8.5.
536 ascend {|v| vs << v }
537 vs.reverse_each {|v| yield v }
541 # Iterates over and yields a new Pathname object
542 # for each element in the given path in ascending order.
544 # Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
545 # #<Pathname:/path/to/some/file.rb>
546 # #<Pathname:/path/to/some>
547 # #<Pathname:/path/to>
551 # Pathname.new('path/to/some/file.rb').ascend {|v| p v}
552 # #<Pathname:path/to/some/file.rb>
553 # #<Pathname:path/to/some>
554 # #<Pathname:path/to>
557 # It doesn't access actual filesystem.
559 # This method is available since 1.8.5.
564 while r = chop_basename(path)
567 yield self.class.new(del_trailing_separator(path))
572 # Pathname#+ appends a pathname fragment to this one to produce a new Pathname
575 # p1 = Pathname.new("/usr") # Pathname:/usr
576 # p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
577 # p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
579 # This method doesn't access the file system; it is pure string manipulation.
582 other = Pathname.new(other) unless Pathname === other
583 Pathname.new(plus(@path, other.to_s))
586 def plus(path1, path2) # -> path
590 while r2 = chop_basename(prefix2)
591 prefix2, basename2 = r2
592 index_list2.unshift prefix2.length
593 basename_list2.unshift basename2
595 return path2 if prefix2 != ''
598 while !basename_list2.empty? && basename_list2.first == '.'
602 break unless r1 = chop_basename(prefix1)
603 prefix1, basename1 = r1
604 next if basename1 == '.'
605 if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
606 prefix1 = prefix1 + basename1
612 r1 = chop_basename(prefix1)
613 if !r1 && /#{SEPARATOR_PAT}/o =~ File.basename(prefix1)
614 while !basename_list2.empty? && basename_list2.first == '..'
619 if !basename_list2.empty?
620 suffix2 = path2[index_list2.first..-1]
621 r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
623 r1 ? prefix1 : File.dirname(prefix1)
629 # Pathname#join joins pathnames.
631 # <tt>path0.join(path1, ..., pathN)</tt> is the same as
632 # <tt>path0 + path1 + ... + pathN</tt>.
637 result = Pathname.new(result) unless Pathname === result
638 return result if result.absolute?
639 args.reverse_each {|arg|
640 arg = Pathname.new(arg) unless Pathname === arg
641 result = arg + result
642 return result if result.absolute?
648 # Returns the children of the directory (files and subdirectories, not
649 # recursive) as an array of Pathname objects. By default, the returned
650 # pathnames will have enough information to access the files. If you set
651 # +with_directory+ to +false+, then the returned pathnames will contain the
655 # p = Pathname("/usr/lib/ruby/1.8")
657 # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
658 # Pathname:/usr/lib/ruby/1.8/Env.rb,
659 # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
661 # # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
663 # Note that the result never contain the entries <tt>.</tt> and <tt>..</tt> in
664 # the directory because they are not children.
666 # This method has existed since 1.8.1.
668 def children(with_directory=true)
669 with_directory = false if @path == '.'
671 Dir.foreach(@path) {|e|
672 next if e == '.' || e == '..'
674 result << self.class.new(File.join(@path, e))
676 result << self.class.new(e)
683 # #relative_path_from returns a relative path from the argument to the
684 # receiver. If +self+ is absolute, the argument must be absolute too. If
685 # +self+ is relative, the argument must be relative too.
687 # #relative_path_from doesn't access the filesystem. It assumes no symlinks.
689 # ArgumentError is raised when it cannot find a relative path.
691 # This method has existed since 1.8.1.
693 def relative_path_from(base_directory)
694 dest_directory = self.cleanpath.to_s
695 base_directory = base_directory.cleanpath.to_s
696 dest_prefix = dest_directory
698 while r = chop_basename(dest_prefix)
699 dest_prefix, basename = r
700 dest_names.unshift basename if basename != '.'
702 base_prefix = base_directory
704 while r = chop_basename(base_prefix)
705 base_prefix, basename = r
706 base_names.unshift basename if basename != '.'
708 if dest_prefix != base_prefix
709 raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
711 while !dest_names.empty? &&
712 !base_names.empty? &&
713 dest_names.first == base_names.first
717 if base_names.include? '..'
718 raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
720 base_names.fill('..')
721 relpath_names = base_names + dest_names
722 if relpath_names.empty?
725 Pathname.new(File.join(*relpath_names))
730 class Pathname # * IO *
732 # #each_line iterates over the line in the file. It yields a String object
735 # This method has existed since 1.8.1.
737 def each_line(*args, &block) # :yield: line
738 IO.foreach(@path, *args, &block)
741 # Pathname#foreachline is *obsoleted* at 1.8.1. Use #each_line.
742 def foreachline(*args, &block)
743 warn "Pathname#foreachline is obsoleted. Use Pathname#each_line."
744 each_line(*args, &block)
747 # See <tt>IO.read</tt>. Returns all the bytes from the file, or the first +N+
749 def read(*args) IO.read(@path, *args) end
751 # See <tt>IO.readlines</tt>. Returns all the lines from the file.
752 def readlines(*args) IO.readlines(@path, *args) end
754 # See <tt>IO.sysopen</tt>.
755 def sysopen(*args) IO.sysopen(@path, *args) end
759 class Pathname # * File *
761 # See <tt>File.atime</tt>. Returns last access time.
762 def atime() File.atime(@path) end
764 # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
765 def ctime() File.ctime(@path) end
767 # See <tt>File.mtime</tt>. Returns last modification time.
768 def mtime() File.mtime(@path) end
770 # See <tt>File.chmod</tt>. Changes permissions.
771 def chmod(mode) File.chmod(mode, @path) end
773 # See <tt>File.lchmod</tt>.
774 def lchmod(mode) File.lchmod(mode, @path) end
776 # See <tt>File.chown</tt>. Change owner and group of file.
777 def chown(owner, group) File.chown(owner, group, @path) end
779 # See <tt>File.lchown</tt>.
780 def lchown(owner, group) File.lchown(owner, group, @path) end
782 # See <tt>File.fnmatch</tt>. Return +true+ if the receiver matches the given
784 def fnmatch(pattern, *args) File.fnmatch(pattern, @path, *args) end
786 # See <tt>File.fnmatch?</tt> (same as #fnmatch).
787 def fnmatch?(pattern, *args) File.fnmatch?(pattern, @path, *args) end
789 # See <tt>File.ftype</tt>. Returns "type" of file ("file", "directory",
791 def ftype() File.ftype(@path) end
793 # See <tt>File.link</tt>. Creates a hard link.
794 def make_link(old) File.link(old, @path) end
796 # See <tt>File.open</tt>. Opens the file for reading or writing.
797 def open(*args, &block) # :yield: file
798 File.open(@path, *args, &block)
801 # See <tt>File.readlink</tt>. Read symbolic link.
802 def readlink() self.class.new(File.readlink(@path)) end
804 # See <tt>File.rename</tt>. Rename the file.
805 def rename(to) File.rename(@path, to) end
807 # See <tt>File.stat</tt>. Returns a <tt>File::Stat</tt> object.
808 def stat() File.stat(@path) end
810 # See <tt>File.lstat</tt>.
811 def lstat() File.lstat(@path) end
813 # See <tt>File.symlink</tt>. Creates a symbolic link.
814 def make_symlink(old) File.symlink(old, @path) end
816 # See <tt>File.truncate</tt>. Truncate the file to +length+ bytes.
817 def truncate(length) File.truncate(@path, length) end
819 # See <tt>File.utime</tt>. Update the access and modification times.
820 def utime(atime, mtime) File.utime(atime, mtime, @path) end
822 # See <tt>File.basename</tt>. Returns the last component of the path.
823 def basename(*args) self.class.new(File.basename(@path, *args)) end
825 # See <tt>File.dirname</tt>. Returns all but the last component of the path.
826 def dirname() self.class.new(File.dirname(@path)) end
828 # See <tt>File.extname</tt>. Returns the file's extension.
829 def extname() File.extname(@path) end
831 # See <tt>File.expand_path</tt>.
832 def expand_path(*args) self.class.new(File.expand_path(@path, *args)) end
834 # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
836 def split() File.split(@path).map {|f| self.class.new(f) } end
838 # Pathname#link is confusing and *obsoleted* because the receiver/argument
839 # order is inverted to corresponding system call.
841 warn 'Pathname#link is obsoleted. Use Pathname#make_link.'
842 File.link(old, @path)
845 # Pathname#symlink is confusing and *obsoleted* because the receiver/argument
846 # order is inverted to corresponding system call.
848 warn 'Pathname#symlink is obsoleted. Use Pathname#make_symlink.'
849 File.symlink(old, @path)
854 class Pathname # * FileTest *
856 # See <tt>FileTest.blockdev?</tt>.
857 def blockdev?() FileTest.blockdev?(@path) end
859 # See <tt>FileTest.chardev?</tt>.
860 def chardev?() FileTest.chardev?(@path) end
862 # See <tt>FileTest.executable?</tt>.
863 def executable?() FileTest.executable?(@path) end
865 # See <tt>FileTest.executable_real?</tt>.
866 def executable_real?() FileTest.executable_real?(@path) end
868 # See <tt>FileTest.exist?</tt>.
869 def exist?() FileTest.exist?(@path) end
871 # See <tt>FileTest.grpowned?</tt>.
872 def grpowned?() FileTest.grpowned?(@path) end
874 # See <tt>FileTest.directory?</tt>.
875 def directory?() FileTest.directory?(@path) end
877 # See <tt>FileTest.file?</tt>.
878 def file?() FileTest.file?(@path) end
880 # See <tt>FileTest.pipe?</tt>.
881 def pipe?() FileTest.pipe?(@path) end
883 # See <tt>FileTest.socket?</tt>.
884 def socket?() FileTest.socket?(@path) end
886 # See <tt>FileTest.owned?</tt>.
887 def owned?() FileTest.owned?(@path) end
889 # See <tt>FileTest.readable?</tt>.
890 def readable?() FileTest.readable?(@path) end
892 # See <tt>FileTest.world_readable?</tt>.
893 def world_readable?() FileTest.world_readable?(@path) end
895 # See <tt>FileTest.readable_real?</tt>.
896 def readable_real?() FileTest.readable_real?(@path) end
898 # See <tt>FileTest.setuid?</tt>.
899 def setuid?() FileTest.setuid?(@path) end
901 # See <tt>FileTest.setgid?</tt>.
902 def setgid?() FileTest.setgid?(@path) end
904 # See <tt>FileTest.size</tt>.
905 def size() FileTest.size(@path) end
907 # See <tt>FileTest.size?</tt>.
908 def size?() FileTest.size?(@path) end
910 # See <tt>FileTest.sticky?</tt>.
911 def sticky?() FileTest.sticky?(@path) end
913 # See <tt>FileTest.symlink?</tt>.
914 def symlink?() FileTest.symlink?(@path) end
916 # See <tt>FileTest.writable?</tt>.
917 def writable?() FileTest.writable?(@path) end
919 # See <tt>FileTest.world_writable?</tt>.
920 def world_writable?() FileTest.world_writable?(@path) end
922 # See <tt>FileTest.writable_real?</tt>.
923 def writable_real?() FileTest.writable_real?(@path) end
925 # See <tt>FileTest.zero?</tt>.
926 def zero?() FileTest.zero?(@path) end
930 class Pathname # * Dir *
931 # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
932 def Pathname.glob(*args) # :yield: p
934 Dir.glob(*args) {|f| yield self.new(f) }
936 Dir.glob(*args).map {|f| self.new(f) }
940 # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
941 def Pathname.getwd() self.new(Dir.getwd) end
942 class << self; alias pwd getwd end
944 # Pathname#chdir is *obsoleted* at 1.8.1.
946 warn "Pathname#chdir is obsoleted. Use Dir.chdir."
947 Dir.chdir(@path, &block)
950 # Pathname#chroot is *obsoleted* at 1.8.1.
952 warn "Pathname#chroot is obsoleted. Use Dir.chroot."
956 # Return the entries (files and subdirectories) in the directory, each as a
958 def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
960 # Iterates over the entries (files and subdirectories) in the directory. It
961 # yields a Pathname object for each entry.
963 # This method has existed since 1.8.1.
964 def each_entry(&block) # :yield: p
965 Dir.foreach(@path) {|f| yield self.class.new(f) }
968 # Pathname#dir_foreach is *obsoleted* at 1.8.1.
969 def dir_foreach(*args, &block)
970 warn "Pathname#dir_foreach is obsoleted. Use Pathname#each_entry."
971 each_entry(*args, &block)
974 # See <tt>Dir.mkdir</tt>. Create the referenced directory.
975 def mkdir(*args) Dir.mkdir(@path, *args) end
977 # See <tt>Dir.rmdir</tt>. Remove the referenced directory.
978 def rmdir() Dir.rmdir(@path) end
980 # See <tt>Dir.open</tt>.
981 def opendir(&block) # :yield: dir
982 Dir.open(@path, &block)
987 class Pathname # * Find *
989 # Pathname#find is an iterator to traverse a directory tree in a depth first
990 # manner. It yields a Pathname for each file under "this" directory.
992 # Since it is implemented by <tt>find.rb</tt>, <tt>Find.prune</tt> can be used
993 # to control the traverse.
995 # If +self+ is <tt>.</tt>, yielded pathnames begin with a filename in the
996 # current directory, not <tt>./</tt>.
998 def find(&block) # :yield: p
1001 Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
1003 Find.find(@path) {|f| yield self.class.new(f) }
1009 class Pathname # * FileUtils *
1010 # See <tt>FileUtils.mkpath</tt>. Creates a full path, including any
1011 # intermediate directories that don't yet exist.
1014 FileUtils.mkpath(@path)
1018 # See <tt>FileUtils.rm_r</tt>. Deletes a directory and all beneath it.
1020 # The name "rmtree" is borrowed from File::Path of Perl.
1021 # File::Path provides "mkpath" and "rmtree".
1023 FileUtils.rm_r(@path)
1029 class Pathname # * mixed *
1030 # Removes a file or directory, using <tt>File.unlink</tt> or
1031 # <tt>Dir.unlink</tt> as necessary.
1035 rescue Errno::ENOTDIR
1041 # This method is *obsoleted* at 1.8.1. Use #each_line or #each_entry.
1042 def foreach(*args, &block)
1043 warn "Pathname#foreach is obsoleted. Use each_line or each_entry."
1044 if FileTest.directory? @path
1045 # For polymorphism between Dir.foreach and IO.foreach,
1046 # Pathname#foreach doesn't yield Pathname object.
1047 Dir.foreach(@path, *args, &block)
1049 IO.foreach(@path, *args, &block)
1055 # create a pathname object.
1057 # This method is available since 1.8.5.
1058 def Pathname(path) # :doc: