Feature #11052 ยป 0001-Blockless-Pathname-ascend-and-descend-return-Enumera.patch
| ChangeLog | ||
|---|---|---|
|
Thu Apr 9 07:18:48 2015 Piotr Szotkowski <chastell@chastell.net>
|
||
|
* ext/pathname/lib/pathname.rb: blockless Pathname#ascend
|
||
|
and Pathname#descend return Enumerator. [Feature #11052]
|
||
|
* test/pathname/test_pathname.rb: test the above.
|
||
|
Wed Apr 8 17:45:02 2015 Shannon Skipper <shannonskipper@gmail.com>
|
||
|
* version.c (Init_version): the version of the engine or
|
||
| ext/pathname/lib/pathname.rb | ||
|---|---|---|
|
# #<Pathname:path/to/some>
|
||
|
# #<Pathname:path/to/some/file.rb>
|
||
|
#
|
||
|
# Returns an Enumerator if no block was given.
|
||
|
#
|
||
|
# enum = Pathname.new("/usr/bin/ruby").descend
|
||
|
# # ... do stuff ...
|
||
|
# enum.each { |e| ... }
|
||
|
# # yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
|
||
|
#
|
||
|
# It doesn't access the filesystem.
|
||
|
#
|
||
|
def descend
|
||
|
return to_enum(__method__) unless block_given?
|
||
|
vs = []
|
||
|
ascend {|v| vs << v }
|
||
|
vs.reverse_each {|v| yield v }
|
||
| ... | ... | |
|
# #<Pathname:path/to>
|
||
|
# #<Pathname:path>
|
||
|
#
|
||
|
# Returns an Enumerator if no block was given.
|
||
|
#
|
||
|
# enum = Pathname.new("/usr/bin/ruby").ascend
|
||
|
# # ... do stuff ...
|
||
|
# enum.each { |e| ... }
|
||
|
# # yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
|
||
|
#
|
||
|
# It doesn't access the filesystem.
|
||
|
#
|
||
|
def ascend
|
||
|
return to_enum(__method__) unless block_given?
|
||
|
path = @path
|
||
|
yield self
|
||
|
while r = chop_basename(path)
|
||
| test/pathname/test_pathname.rb | ||
|---|---|---|
|
end
|
||
|
def descend(path)
|
||
|
Pathname.new(path).enum_for(:descend).map {|v| v.to_s }
|
||
|
Pathname.new(path).descend.map(&:to_s)
|
||
|
end
|
||
|
defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
|
||
| ... | ... | |
|
defassert(:descend, %w[a/], "a/")
|
||
|
def ascend(path)
|
||
|
Pathname.new(path).enum_for(:ascend).map {|v| v.to_s }
|
||
|
Pathname.new(path).ascend.map(&:to_s)
|
||
|
end
|
||
|
defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
|
||
| ... | ... | |
|
defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
|
||
|
defassert(:ascend, %w[a/], "a/")
|
||
|
def test_blockless_ascend_is_enumerator
|
||
|
assert_kind_of(Enumerator, Pathname.new('a').ascend)
|
||
|
end
|
||
|
def test_blockless_descend_is_enumerator
|
||
|
assert_kind_of(Enumerator, Pathname.new('a').descend)
|
||
|
end
|
||
|
def test_initialize
|
||
|
p1 = Pathname.new('a')
|
||
|
assert_equal('a', p1.to_s)
|
||