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_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
259 SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
261 SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
262 SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
265 # Return a pathname which the extension of the basename is substituted by
268 # If self has no extension part, <i>repl</i> is appended.
270 ext = File.extname(@path)
271 self.class.new(@path.chomp(ext) + repl)
274 # chop_basename(path) -> [pre-basename, basename] or nil
275 def chop_basename(path)
276 base = File.basename(path)
277 if /\A#{SEPARATOR_PAT}?\z/ =~ base
280 return path[0, path.rindex(base)], base
283 private :chop_basename
285 # split_names(path) -> prefix, [name, ...]
286 def split_names(path)
288 while r = chop_basename(path)
290 names.unshift basename
296 def prepend_prefix(prefix, relpath)
299 elsif /#{SEPARATOR_PAT}/ =~ prefix
300 prefix = File.dirname(prefix)
301 prefix = File.join(prefix, "") if File.basename(prefix + 'a') != 'a'
307 private :prepend_prefix
309 # Returns clean pathname of +self+ with consecutive slashes and useless dots
310 # removed. The filesystem is not accessed.
312 # If +consider_symlink+ is +true+, then a more conservative algorithm is used
313 # to avoid breaking symbolic linkages. This may retain more <tt>..</tt>
314 # entries than absolutely necessary, but without accessing the filesystem,
315 # this can't be avoided. See #realpath.
317 def cleanpath(consider_symlink=false)
319 cleanpath_conservative
326 # Clean the path simply by resolving and removing excess "." and ".." entries.
327 # Nothing more, nothing less.
329 def cleanpath_aggressive
333 while r = chop_basename(pre)
347 if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
348 names.shift while names[0] == '..'
350 self.class.new(prepend_prefix(pre, File.join(*names)))
352 private :cleanpath_aggressive
354 # has_trailing_separator?(path) -> bool
355 def has_trailing_separator?(path)
356 if r = chop_basename(path)
358 pre.length + basename.length < path.length
363 private :has_trailing_separator?
365 # add_trailing_separator(path) -> path
366 def add_trailing_separator(path)
367 if File.basename(path + 'a') == 'a'
370 File.join(path, "") # xxx: Is File.join is appropriate to add separator?
373 private :add_trailing_separator
375 def del_trailing_separator(path)
376 if r = chop_basename(path)
379 elsif /#{SEPARATOR_PAT}+\z/o =~ path
380 $` + File.dirname(path)[/#{SEPARATOR_PAT}*\z/o]
385 private :del_trailing_separator
387 def cleanpath_conservative
391 while r = chop_basename(pre)
393 names.unshift base if base != '.'
395 if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
396 names.shift while names[0] == '..'
399 self.class.new(File.dirname(pre))
401 if names.last != '..' && File.basename(path) == '.'
404 result = prepend_prefix(pre, File.join(*names))
405 if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
406 self.class.new(add_trailing_separator(result))
408 self.class.new(result)
412 private :cleanpath_conservative
414 def realpath_rec(prefix, unresolved, h)
416 until unresolved.empty?
423 path = prepend_prefix(prefix, File.join(*(resolved + [n])))
425 if h[path] == :resolving
426 raise Errno::ELOOP.new(path)
428 prefix, *resolved = h[path]
434 link_prefix, link_names = split_names(File.readlink(path))
436 prefix, *resolved = h[path] = realpath_rec(prefix, resolved + link_names, h)
438 prefix, *resolved = h[path] = realpath_rec(link_prefix, link_names, h)
442 h[path] = [prefix, *resolved]
447 return prefix, *resolved
449 private :realpath_rec
452 # Returns a real (absolute) pathname of +self+ in the actual filesystem.
453 # The real pathname doesn't contain symlinks or useless dots.
455 # No arguments should be given; the old behaviour is *obsoleted*.
459 prefix, names = split_names(path)
461 prefix, names2 = split_names(Dir.pwd)
462 names = names2 + names
464 prefix, *names = realpath_rec(prefix, names, {})
465 self.class.new(prepend_prefix(prefix, File.join(*names)))
468 # #parent returns the parent directory.
470 # This is same as <tt>self + '..'</tt>.
475 # #mountpoint? returns +true+ if <tt>self</tt> points to a mountpoint.
479 stat2 = self.parent.lstat
480 stat1.dev == stat2.dev && stat1.ino == stat2.ino ||
481 stat1.dev != stat2.dev
488 # #root? is a predicate for root directories. I.e. it returns +true+ if the
489 # pathname consists of consecutive slashes.
491 # It doesn't access actual filesystem. So it may return +false+ for some
492 # pathnames which points to roots such as <tt>/usr/..</tt>.
495 !!(chop_basename(@path) == nil && /#{SEPARATOR_PAT}/o =~ @path)
498 # Predicate method for testing whether a path is absolute.
499 # It returns +true+ if the pathname begins with a slash.
504 # The opposite of #absolute?
507 while r = chop_basename(path)
514 # Iterates over each component of the path.
516 # Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
517 # # yields "usr", "bin", and "ruby".
519 def each_filename # :yield: filename
520 prefix, names = split_names(@path)
521 names.each {|filename| yield filename }
525 # Iterates over and yields a new Pathname object
526 # for each element in the given path in descending order.
528 # Pathname.new('/path/to/some/file.rb').descend {|v| p v}
531 # #<Pathname:/path/to>
532 # #<Pathname:/path/to/some>
533 # #<Pathname:/path/to/some/file.rb>
535 # Pathname.new('path/to/some/file.rb').descend {|v| p v}
537 # #<Pathname:path/to>
538 # #<Pathname:path/to/some>
539 # #<Pathname:path/to/some/file.rb>
541 # It doesn't access actual filesystem.
543 # This method is available since 1.8.5.
547 ascend {|v| vs << v }
548 vs.reverse_each {|v| yield v }
552 # Iterates over and yields a new Pathname object
553 # for each element in the given path in ascending order.
555 # Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
556 # #<Pathname:/path/to/some/file.rb>
557 # #<Pathname:/path/to/some>
558 # #<Pathname:/path/to>
562 # Pathname.new('path/to/some/file.rb').ascend {|v| p v}
563 # #<Pathname:path/to/some/file.rb>
564 # #<Pathname:path/to/some>
565 # #<Pathname:path/to>
568 # It doesn't access actual filesystem.
570 # This method is available since 1.8.5.
575 while r = chop_basename(path)
578 yield self.class.new(del_trailing_separator(path))
583 # Pathname#+ appends a pathname fragment to this one to produce a new Pathname
586 # p1 = Pathname.new("/usr") # Pathname:/usr
587 # p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
588 # p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
590 # This method doesn't access the file system; it is pure string manipulation.
593 other = Pathname.new(other) unless Pathname === other
594 Pathname.new(plus(@path, other.to_s))
597 def plus(path1, path2) # -> path
601 while r2 = chop_basename(prefix2)
602 prefix2, basename2 = r2
603 index_list2.unshift prefix2.length
604 basename_list2.unshift basename2
606 return path2 if prefix2 != ''
609 while !basename_list2.empty? && basename_list2.first == '.'
613 break unless r1 = chop_basename(prefix1)
614 prefix1, basename1 = r1
615 next if basename1 == '.'
616 if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
617 prefix1 = prefix1 + basename1
623 r1 = chop_basename(prefix1)
624 if !r1 && /#{SEPARATOR_PAT}/o =~ File.basename(prefix1)
625 while !basename_list2.empty? && basename_list2.first == '..'
630 if !basename_list2.empty?
631 suffix2 = path2[index_list2.first..-1]
632 r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
634 r1 ? prefix1 : File.dirname(prefix1)
640 # Pathname#join joins pathnames.
642 # <tt>path0.join(path1, ..., pathN)</tt> is the same as
643 # <tt>path0 + path1 + ... + pathN</tt>.
648 result = Pathname.new(result) unless Pathname === result
649 return result if result.absolute?
650 args.reverse_each {|arg|
651 arg = Pathname.new(arg) unless Pathname === arg
652 result = arg + result
653 return result if result.absolute?
659 # Returns the children of the directory (files and subdirectories, not
660 # recursive) as an array of Pathname objects. By default, the returned
661 # pathnames will have enough information to access the files. If you set
662 # +with_directory+ to +false+, then the returned pathnames will contain the
666 # p = Pathname("/usr/lib/ruby/1.8")
668 # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
669 # Pathname:/usr/lib/ruby/1.8/Env.rb,
670 # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
672 # # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
674 # Note that the result never contain the entries <tt>.</tt> and <tt>..</tt> in
675 # the directory because they are not children.
677 # This method has existed since 1.8.1.
679 def children(with_directory=true)
680 with_directory = false if @path == '.'
682 Dir.foreach(@path) {|e|
683 next if e == '.' || e == '..'
685 result << self.class.new(File.join(@path, e))
687 result << self.class.new(e)
694 # #relative_path_from returns a relative path from the argument to the
695 # receiver. If +self+ is absolute, the argument must be absolute too. If
696 # +self+ is relative, the argument must be relative too.
698 # #relative_path_from doesn't access the filesystem. It assumes no symlinks.
700 # ArgumentError is raised when it cannot find a relative path.
702 # This method has existed since 1.8.1.
704 def relative_path_from(base_directory)
705 dest_directory = self.cleanpath.to_s
706 base_directory = base_directory.cleanpath.to_s
707 dest_prefix = dest_directory
709 while r = chop_basename(dest_prefix)
710 dest_prefix, basename = r
711 dest_names.unshift basename if basename != '.'
713 base_prefix = base_directory
715 while r = chop_basename(base_prefix)
716 base_prefix, basename = r
717 base_names.unshift basename if basename != '.'
719 if dest_prefix != base_prefix
720 raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
722 while !dest_names.empty? &&
723 !base_names.empty? &&
724 dest_names.first == base_names.first
728 if base_names.include? '..'
729 raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
731 base_names.fill('..')
732 relpath_names = base_names + dest_names
733 if relpath_names.empty?
736 Pathname.new(File.join(*relpath_names))
741 class Pathname # * IO *
743 # #each_line iterates over the line in the file. It yields a String object
746 # This method has existed since 1.8.1.
748 def each_line(*args, &block) # :yield: line
749 IO.foreach(@path, *args, &block)
752 # Pathname#foreachline is *obsoleted* at 1.8.1. Use #each_line.
753 def foreachline(*args, &block)
754 warn "Pathname#foreachline is obsoleted. Use Pathname#each_line."
755 each_line(*args, &block)
758 # See <tt>IO.read</tt>. Returns all the bytes from the file, or the first +N+
760 def read(*args) IO.read(@path, *args) end
762 # See <tt>IO.readlines</tt>. Returns all the lines from the file.
763 def readlines(*args) IO.readlines(@path, *args) end
765 # See <tt>IO.sysopen</tt>.
766 def sysopen(*args) IO.sysopen(@path, *args) end
770 class Pathname # * File *
772 # See <tt>File.atime</tt>. Returns last access time.
773 def atime() File.atime(@path) end
775 # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
776 def ctime() File.ctime(@path) end
778 # See <tt>File.mtime</tt>. Returns last modification time.
779 def mtime() File.mtime(@path) end
781 # See <tt>File.chmod</tt>. Changes permissions.
782 def chmod(mode) File.chmod(mode, @path) end
784 # See <tt>File.lchmod</tt>.
785 def lchmod(mode) File.lchmod(mode, @path) end
787 # See <tt>File.chown</tt>. Change owner and group of file.
788 def chown(owner, group) File.chown(owner, group, @path) end
790 # See <tt>File.lchown</tt>.
791 def lchown(owner, group) File.lchown(owner, group, @path) end
793 # See <tt>File.fnmatch</tt>. Return +true+ if the receiver matches the given
795 def fnmatch(pattern, *args) File.fnmatch(pattern, @path, *args) end
797 # See <tt>File.fnmatch?</tt> (same as #fnmatch).
798 def fnmatch?(pattern, *args) File.fnmatch?(pattern, @path, *args) end
800 # See <tt>File.ftype</tt>. Returns "type" of file ("file", "directory",
802 def ftype() File.ftype(@path) end
804 # See <tt>File.link</tt>. Creates a hard link.
805 def make_link(old) File.link(old, @path) end
807 # See <tt>File.open</tt>. Opens the file for reading or writing.
808 def open(*args, &block) # :yield: file
809 File.open(@path, *args, &block)
812 # See <tt>File.readlink</tt>. Read symbolic link.
813 def readlink() self.class.new(File.readlink(@path)) end
815 # See <tt>File.rename</tt>. Rename the file.
816 def rename(to) File.rename(@path, to) end
818 # See <tt>File.stat</tt>. Returns a <tt>File::Stat</tt> object.
819 def stat() File.stat(@path) end
821 # See <tt>File.lstat</tt>.
822 def lstat() File.lstat(@path) end
824 # See <tt>File.symlink</tt>. Creates a symbolic link.
825 def make_symlink(old) File.symlink(old, @path) end
827 # See <tt>File.truncate</tt>. Truncate the file to +length+ bytes.
828 def truncate(length) File.truncate(@path, length) end
830 # See <tt>File.utime</tt>. Update the access and modification times.
831 def utime(atime, mtime) File.utime(atime, mtime, @path) end
833 # See <tt>File.basename</tt>. Returns the last component of the path.
834 def basename(*args) self.class.new(File.basename(@path, *args)) end
836 # See <tt>File.dirname</tt>. Returns all but the last component of the path.
837 def dirname() self.class.new(File.dirname(@path)) end
839 # See <tt>File.extname</tt>. Returns the file's extension.
840 def extname() File.extname(@path) end
842 # See <tt>File.expand_path</tt>.
843 def expand_path(*args) self.class.new(File.expand_path(@path, *args)) end
845 # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
847 def split() File.split(@path).map {|f| self.class.new(f) } end
849 # Pathname#link is confusing and *obsoleted* because the receiver/argument
850 # order is inverted to corresponding system call.
852 warn 'Pathname#link is obsoleted. Use Pathname#make_link.'
853 File.link(old, @path)
856 # Pathname#symlink is confusing and *obsoleted* because the receiver/argument
857 # order is inverted to corresponding system call.
859 warn 'Pathname#symlink is obsoleted. Use Pathname#make_symlink.'
860 File.symlink(old, @path)
865 class Pathname # * FileTest *
867 # See <tt>FileTest.blockdev?</tt>.
868 def blockdev?() FileTest.blockdev?(@path) end
870 # See <tt>FileTest.chardev?</tt>.
871 def chardev?() FileTest.chardev?(@path) end
873 # See <tt>FileTest.executable?</tt>.
874 def executable?() FileTest.executable?(@path) end
876 # See <tt>FileTest.executable_real?</tt>.
877 def executable_real?() FileTest.executable_real?(@path) end
879 # See <tt>FileTest.exist?</tt>.
880 def exist?() FileTest.exist?(@path) end
882 # See <tt>FileTest.grpowned?</tt>.
883 def grpowned?() FileTest.grpowned?(@path) end
885 # See <tt>FileTest.directory?</tt>.
886 def directory?() FileTest.directory?(@path) end
888 # See <tt>FileTest.file?</tt>.
889 def file?() FileTest.file?(@path) end
891 # See <tt>FileTest.pipe?</tt>.
892 def pipe?() FileTest.pipe?(@path) end
894 # See <tt>FileTest.socket?</tt>.
895 def socket?() FileTest.socket?(@path) end
897 # See <tt>FileTest.owned?</tt>.
898 def owned?() FileTest.owned?(@path) end
900 # See <tt>FileTest.readable?</tt>.
901 def readable?() FileTest.readable?(@path) end
903 # See <tt>FileTest.world_readable?</tt>.
904 def world_readable?() FileTest.world_readable?(@path) end
906 # See <tt>FileTest.readable_real?</tt>.
907 def readable_real?() FileTest.readable_real?(@path) end
909 # See <tt>FileTest.setuid?</tt>.
910 def setuid?() FileTest.setuid?(@path) end
912 # See <tt>FileTest.setgid?</tt>.
913 def setgid?() FileTest.setgid?(@path) end
915 # See <tt>FileTest.size</tt>.
916 def size() FileTest.size(@path) end
918 # See <tt>FileTest.size?</tt>.
919 def size?() FileTest.size?(@path) end
921 # See <tt>FileTest.sticky?</tt>.
922 def sticky?() FileTest.sticky?(@path) end
924 # See <tt>FileTest.symlink?</tt>.
925 def symlink?() FileTest.symlink?(@path) end
927 # See <tt>FileTest.writable?</tt>.
928 def writable?() FileTest.writable?(@path) end
930 # See <tt>FileTest.world_writable?</tt>.
931 def world_writable?() FileTest.world_writable?(@path) end
933 # See <tt>FileTest.writable_real?</tt>.
934 def writable_real?() FileTest.writable_real?(@path) end
936 # See <tt>FileTest.zero?</tt>.
937 def zero?() FileTest.zero?(@path) end
941 class Pathname # * Dir *
942 # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
943 def Pathname.glob(*args) # :yield: p
945 Dir.glob(*args) {|f| yield self.new(f) }
947 Dir.glob(*args).map {|f| self.new(f) }
951 # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
952 def Pathname.getwd() self.new(Dir.getwd) end
953 class << self; alias pwd getwd end
955 # Pathname#chdir is *obsoleted* at 1.8.1.
957 warn "Pathname#chdir is obsoleted. Use Dir.chdir."
958 Dir.chdir(@path, &block)
961 # Pathname#chroot is *obsoleted* at 1.8.1.
963 warn "Pathname#chroot is obsoleted. Use Dir.chroot."
967 # Return the entries (files and subdirectories) in the directory, each as a
969 def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
971 # Iterates over the entries (files and subdirectories) in the directory. It
972 # yields a Pathname object for each entry.
974 # This method has existed since 1.8.1.
975 def each_entry(&block) # :yield: p
976 Dir.foreach(@path) {|f| yield self.class.new(f) }
979 # Pathname#dir_foreach is *obsoleted* at 1.8.1.
980 def dir_foreach(*args, &block)
981 warn "Pathname#dir_foreach is obsoleted. Use Pathname#each_entry."
982 each_entry(*args, &block)
985 # See <tt>Dir.mkdir</tt>. Create the referenced directory.
986 def mkdir(*args) Dir.mkdir(@path, *args) end
988 # See <tt>Dir.rmdir</tt>. Remove the referenced directory.
989 def rmdir() Dir.rmdir(@path) end
991 # See <tt>Dir.open</tt>.
992 def opendir(&block) # :yield: dir
993 Dir.open(@path, &block)
998 class Pathname # * Find *
1000 # Pathname#find is an iterator to traverse a directory tree in a depth first
1001 # manner. It yields a Pathname for each file under "this" directory.
1003 # Since it is implemented by <tt>find.rb</tt>, <tt>Find.prune</tt> can be used
1004 # to control the traverse.
1006 # If +self+ is <tt>.</tt>, yielded pathnames begin with a filename in the
1007 # current directory, not <tt>./</tt>.
1009 def find(&block) # :yield: p
1012 Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
1014 Find.find(@path) {|f| yield self.class.new(f) }
1020 class Pathname # * FileUtils *
1021 # See <tt>FileUtils.mkpath</tt>. Creates a full path, including any
1022 # intermediate directories that don't yet exist.
1025 FileUtils.mkpath(@path)
1029 # See <tt>FileUtils.rm_r</tt>. Deletes a directory and all beneath it.
1031 # The name "rmtree" is borrowed from File::Path of Perl.
1032 # File::Path provides "mkpath" and "rmtree".
1034 FileUtils.rm_r(@path)
1040 class Pathname # * mixed *
1041 # Removes a file or directory, using <tt>File.unlink</tt> or
1042 # <tt>Dir.unlink</tt> as necessary.
1046 rescue Errno::ENOTDIR
1052 # This method is *obsoleted* at 1.8.1. Use #each_line or #each_entry.
1053 def foreach(*args, &block)
1054 warn "Pathname#foreach is obsoleted. Use each_line or each_entry."
1055 if FileTest.directory? @path
1056 # For polymorphism between Dir.foreach and IO.foreach,
1057 # Pathname#foreach doesn't yield Pathname object.
1058 Dir.foreach(@path, *args, &block)
1060 IO.foreach(@path, *args, &block)
1066 # create a pathname object.
1068 # This method is available since 1.8.5.
1069 def Pathname(path) # :doc: