1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

lib/pathname.rb (Pathname#+): re-implemented to resolve ".." in

beginning of the argument.
(Pathname#join): concatenate from the last argument.
(Pathname#parent): just use Pathname#+.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2003-12-21 07:28:59 +00:00
parent da99e407fb
commit 3e67076db0
2 changed files with 68 additions and 27 deletions

View file

@ -1,3 +1,10 @@
Sun Dec 21 16:25:10 2003 Tanaka Akira <akr@m17n.org>
* lib/pathname.rb (Pathname#+): re-implemented to resolve ".." in
beginning of the argument.
(Pathname#join): concatenate from the last argument.
(Pathname#parent): just use Pathname#+.
Sun Dec 21 00:12:37 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> Sun Dec 21 00:12:37 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk.rb: add new methods (TkScrollbar#assign, assign_list) * ext/tk/lib/tk.rb: add new methods (TkScrollbar#assign, assign_list)

View file

@ -173,14 +173,9 @@ class Pathname
# parent method returns parent directory. # parent method returns parent directory.
# #
# If self is `.', `..' is returned. # This is same as self + '..'.
# Otherwise, `..' is joined to self.
def parent def parent
if @path == '.' self + '..'
Pathname.new('..')
else
self.join('..')
end
end end
# mountpoint? method returns true if self points a mountpoint. # mountpoint? method returns true if self points a mountpoint.
@ -222,22 +217,37 @@ class Pathname
@path.scan(%r{[^/]+}) { yield $& } @path.scan(%r{[^/]+}) { yield $& }
end end
# Pathname#+ return new pathname which is concatenated with self and # Pathname#+ concatenates self and an argument.
# an argument. # I.e. a result is basically same as the argument but the base directory
# If self is the current working directory `.' or # is changed to self if the argument is relative.
# the argument is absolute pathname, #
# the argument is just returned. # Pathname#+ doesn't access actual filesystem.
# If the argument is `.', self is returned.
def +(other) def +(other)
other = Pathname.new(other) unless Pathname === other other = Pathname.new(other) unless Pathname === other
if @path == '.' || other.absolute?
other return other if other.absolute?
elsif other.to_s == '.'
self path1 = @path
elsif %r{/\z} =~ @path path2 = other.to_s
Pathname.new(@path + 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 Pathname.new(path2) if path1 == '.'
return Pathname.new(path1) if path2 == '.'
if %r{/\z} =~ path1
Pathname.new(path1 + path2)
else else
Pathname.new(@path + '/' + other.to_s) Pathname.new(path1 + '/' + path2)
end end
end end
@ -245,8 +255,16 @@ class Pathname
# #
# path0.join(path1, ... pathN) is same as path0 + path1 + ... + pathN. # path0.join(path1, ... pathN) is same as path0 + path1 + ... + pathN.
def join(*args) def join(*args)
args.map! {|arg| Pathname === arg ? arg : Pathname.new(arg) } args.unshift self
args.inject(self) {|pathname, arg| pathname + arg } result = args.pop
result = Pathname.new(result) unless Pathname === result
return result if result.absolute?
args.reverse_each {|arg|
arg = Pathname.new(arg) unless Pathname === arg
result = arg + result
return result if result.absolute?
}
result
end 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
@ -739,12 +757,28 @@ if $0 == __FILE__
assert_relpath_err(".", "..") assert_relpath_err(".", "..")
end end
def assert_pathname_plus(a, b, c)
a = Pathname.new(a)
b = Pathname.new(b)
c = Pathname.new(c)
d = b + c
assert(a == d,
"#{b.inspect} + #{c.inspect}: #{a.inspect} expected but was #{d.inspect}")
end
def test_plus def test_plus
assert_equal(Pathname.new('a/b'), Pathname.new('a') + Pathname.new('b')) assert_pathname_plus('a/b', 'a', 'b')
assert_equal(Pathname.new('a'), Pathname.new('a') + Pathname.new('.')) assert_pathname_plus('a', 'a', '.')
assert_equal(Pathname.new('b'), Pathname.new('.') + Pathname.new('b')) assert_pathname_plus('b', '.', 'b')
assert_equal(Pathname.new('.'), Pathname.new('.') + Pathname.new('.')) assert_pathname_plus('.', '.', '.')
assert_equal(Pathname.new('/b'), Pathname.new('a') + Pathname.new('/b')) assert_pathname_plus('/b', 'a', '/b')
assert_pathname_plus('/', '/', '..')
assert_pathname_plus('.', 'a', '..')
assert_pathname_plus('a', 'a/b', '..')
assert_pathname_plus('/c', '/', '../c')
assert_pathname_plus('c', 'a', '../c')
assert_pathname_plus('a/c', 'a/b', '../c')
end end
end end
end end