(Pathname#parent): if self is `.', return `..'.

(Pathname#children): if self is `.', don't prepend self for a
pathname in a result.
(Pathname#join): re-implemented using Pathname#+.
(Pathname#find): if self is `.', remove `./' prefix of yielding
pathname.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4920 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2003-11-07 14:28:20 +00:00
Родитель ea6b397daa
Коммит 91669219ef
2 изменённых файлов: 51 добавлений и 12 удалений

Просмотреть файл

@ -1,7 +1,13 @@
Fri Nov 7 12:50:28 2003 Tanaka Akira <akr@m17n.org> Fri Nov 7 23:23:04 2003 Tanaka Akira <akr@m17n.org>
* lib/pathname.rb (Pathname#+): if self or the argument is `.', return * lib/pathname.rb (Pathname#+): if self or the argument is `.', return
another. another.
(Pathname#parent): if self is `.', return `..'.
(Pathname#children): if self is `.', don't prepend self for a
pathname in a result.
(Pathname#join): re-implemented using Pathname#+.
(Pathname#find): if self is `.', remove `./' prefix of yielding
pathname.
Fri Nov 7 10:23:24 2003 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Nov 7 10:23:24 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>

Просмотреть файл

@ -167,10 +167,17 @@ class Pathname
end end
end end
# parent method returns parent directory, i.e. ".." is joined at last. # parent method returns parent directory.
#
# If self is `.', `..' is returned.
# Otherwise, `..' is joined to self.
def parent def parent
if @path == '.'
Pathname.new('..')
else
self.join('..') self.join('..')
end end
end
# mountpoint? method returns true if self points a mountpoint. # mountpoint? method returns true if self points a mountpoint.
def mountpoint? def mountpoint?
@ -230,22 +237,36 @@ class Pathname
end end
end end
# Pathname#join joins pathnames.
#
# path0.join(path1, ... pathN) is same as path0 + path1 + ... + pathN.
def join(*args)
args.map! {|arg| Pathname === arg ? arg : Pathname.new(arg) }
args.inject(self) {|pathname, arg| pathname + arg }
end
# Pathname#children returns the children of the directory as an array of # Pathname#children returns the children of the directory as an array of
# pathnames. # pathnames.
# #
# By default, self is prepended to each pathname in the result. # By default, the returned pathname can be used to access the corresponding
# It is disabled if false is given for the optional argument # file in the directory.
# prepend_directory. # This is because the pathname contains self as a prefix unless self is `.'.
# #
# Note that the result never contain '.' and '..' because they are not # If false is given for the optional argument `with_directory',
# child. # just filenames of children is returned.
# In this case, the returned pathname cannot be used directly to access the
# corresponding file when self doesn't point working directory.
#
# Note that the result never contain the entry `.' and `..' in the directory
# because they are not child.
# #
# This method is exist since 1.8.1. # This method is exist since 1.8.1.
def children(prepend_directory=true) def children(with_directory=true)
with_directory = false if @path == '.'
result = [] result = []
Dir.foreach(@path) {|e| Dir.foreach(@path) {|e|
next if e == '.' || e == '..' next if e == '.' || e == '..'
if prepend_directory if with_directory
result << Pathname.new(File.join(@path, e)) result << Pathname.new(File.join(@path, e))
else else
result << Pathname.new(e) result << Pathname.new(e)
@ -348,7 +369,6 @@ class Pathname
def dirname() Pathname.new(File.dirname(@path)) end def dirname() Pathname.new(File.dirname(@path)) end
def extname() File.extname(@path) end def extname() File.extname(@path) end
def expand_path(*args) Pathname.new(File.expand_path(@path, *args)) end def expand_path(*args) Pathname.new(File.expand_path(@path, *args)) end
def join(*args) Pathname.new(File.join(@path, *args)) end
def split() File.split(@path).map {|f| Pathname.new(f) } end def split() File.split(@path).map {|f| Pathname.new(f) } end
end end
@ -427,10 +447,23 @@ end
# Find # Find
class Pathname class Pathname
# Pathname#find is a iterator to traverse directory tree in depth first
# manner. It yields a pathname for each file under the directory which
# is pointed by self.
#
# 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 `./'.
def find(&block) def find(&block)
require 'find' require 'find'
if @path == '.'
Find.find(@path) {|f| yield Pathname.new(f.sub(%r{\A\./}, '')) }
else
Find.find(@path) {|f| yield Pathname.new(f) } Find.find(@path) {|f| yield Pathname.new(f) }
end end
end
end end
# FileUtils # FileUtils