aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-08-28 16:45:11 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-08-28 16:45:11 +0000
commitcd7cc246d2d1d2d14f30b066baa56aff2e0f0fbd (patch)
treea2a86ac005d60e379e378d525060f4c0ff3b355f
parent50e604172eb7e95fe1bdadf37faeeb8a97429bae (diff)
downloadruby-cd7cc246d2d1d2d14f30b066baa56aff2e0f0fbd.tar.gz
* lib/pathname.rb (Pathname#descend): new method.
(Pathname#ascend): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/pathname.rb96
2 files changed, 101 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index f1bb4a6227..bc6cf96bbb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Aug 29 01:43:05 2005 Tanaka Akira <akr@m17n.org>
+
+ * lib/pathname.rb (Pathname#descend): new method.
+ (Pathname#ascend): ditto.
+
Mon Aug 29 00:35:09 2005 Tanaka Akira <akr@m17n.org>
* lib/time.rb: require 'date/format' instead of 'parsedate'.
diff --git a/lib/pathname.rb b/lib/pathname.rb
index fd7fa508b8..d9dabd0ca0 100644
--- a/lib/pathname.rb
+++ b/lib/pathname.rb
@@ -422,6 +422,74 @@ class Pathname
@path.scan(%r{[^/]+}) { yield $& }
end
+ # Iterates over and yields a new Pathname object
+ # for each element in the given path in descending order.
+ #
+ # Pathname.new('/path/to/some/file.rb').descend {|v| p v}
+ # #<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| p v}
+ # #<Pathname:path>
+ # #<Pathname:path/to>
+ # #<Pathname:path/to/some>
+ # #<Pathname:path/to/some/file.rb>
+ #
+ def descend
+ paths = []
+ v = self
+ if absolute?
+ until v.root?
+ paths << v
+ v = v.dirname
+ end
+ paths << v
+ else
+ until v.to_s == '.'
+ paths << v
+ v = v.dirname
+ end
+ end
+ paths.reverse_each {|path| yield path }
+ end
+
+ # Iterates over and yields a new Pathname object
+ # for each element in the given path in ascending order.
+ #
+ # Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
+ # #<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| p v}
+ # #<Pathname:path/to/some/file.rb>
+ # #<Pathname:path/to/some>
+ # #<Pathname:path/to>
+ # #<Pathname:path>
+ #
+ def ascend
+ paths = []
+ v = self
+ if absolute?
+ until v.root?
+ paths << v
+ v = v.dirname
+ end
+ paths << v
+ else
+ until v.to_s == '.'
+ paths << v
+ v = v.dirname
+ end
+ end
+ paths.each {|path| yield path }
+ end
+
#
# Pathname#+ appends a pathname fragment to this one to produce a new Pathname
# object.
@@ -1203,5 +1271,33 @@ if $0 == __FILE__
assert_equal(1, count)
assert_equal(2, result)
end
+
+ def test_descend_abs
+ rs = %w[/ /a /a/b /a/b/c]
+ Pathname.new("/a/b/c").descend {|v|
+ assert_equal(Pathname.new(rs.shift), v)
+ }
+ end
+
+ def test_descend_rel
+ rs = %w[a a/b a/b/c]
+ Pathname.new("a/b/c").descend {|v|
+ assert_equal(Pathname.new(rs.shift), v)
+ }
+ end
+
+ def test_ascend_abs
+ rs = %w[/a/b/c /a/b /a /]
+ Pathname.new("/a/b/c").ascend {|v|
+ assert_equal(Pathname.new(rs.shift), v)
+ }
+ end
+
+ def test_ascend_rel
+ rs = %w[a/b/c a/b a]
+ Pathname.new("a/b/c").ascend {|v|
+ assert_equal(Pathname.new(rs.shift), v)
+ }
+ end
end
end