Skip to content

Commit 8bb581f

Browse files
committed
* ext/pathname/lib/pathname.rb (descend): Blockless form supported.
(ascend): Ditto. [ruby-core:68820] [Feature #11052] Patch by Piotr Szotkowski. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 16917fa commit 8bb581f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon Jun 15 00:14:33 2015 Tanaka Akira <akr@fsij.org>
2+
3+
* ext/pathname/lib/pathname.rb (descend): Blockless form supported.
4+
(ascend): Ditto.
5+
[ruby-core:68820] [Feature #11052] Patch by Piotr Szotkowski.
6+
17
Sun Jun 14 20:09:25 2015 Tanaka Akira <akr@fsij.org>
28

39
* time.c (time_getlocaltime): [DOC] Add examples of valid utc_offset

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ with all sufficient information, see the ChangeLog file.
7878
* OpenSSL::SSL::SSLSocket#accept_nonblock and
7979
OpenSSL::SSL::SSLSocket#connect_nonblock supports `exception: false`.
8080

81+
* Pathname
82+
* Pathname#descend and Pathname#ascend supported blockless form.
83+
[Feature #11052]
84+
8185
* io/wait
8286
* IO#wait_readable no longer checks FIONREAD, it may be used for
8387
non-bytestream IO such as listen sockets.

ext/pathname/lib/pathname.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,17 @@ def each_filename # :yield: filename
278278
# #<Pathname:path/to/some>
279279
# #<Pathname:path/to/some/file.rb>
280280
#
281+
# Returns an Enumerator if no block was given.
282+
#
283+
# enum = Pathname.new("/usr/bin/ruby").descend
284+
# # ... do stuff ...
285+
# enum.each { |e| ... }
286+
# # yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
287+
#
281288
# It doesn't access the filesystem.
282289
#
283290
def descend
291+
return to_enum(__method__) unless block_given?
284292
vs = []
285293
ascend {|v| vs << v }
286294
vs.reverse_each {|v| yield v }
@@ -303,9 +311,17 @@ def descend
303311
# #<Pathname:path/to>
304312
# #<Pathname:path>
305313
#
314+
# Returns an Enumerator if no block was given.
315+
#
316+
# enum = Pathname.new("/usr/bin/ruby").ascend
317+
# # ... do stuff ...
318+
# enum.each { |e| ... }
319+
# # yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
320+
#
306321
# It doesn't access the filesystem.
307322
#
308323
def ascend
324+
return to_enum(__method__) unless block_given?
309325
path = @path
310326
yield self
311327
while r = chop_basename(path)

test/pathname/test_pathname.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def test_realdirpath
430430
end
431431

432432
def descend(path)
433-
Pathname.new(path).enum_for(:descend).map {|v| v.to_s }
433+
Pathname.new(path).descend.map(&:to_s)
434434
end
435435

436436
defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
@@ -439,14 +439,22 @@ def descend(path)
439439
defassert(:descend, %w[a/], "a/")
440440

441441
def ascend(path)
442-
Pathname.new(path).enum_for(:ascend).map {|v| v.to_s }
442+
Pathname.new(path).ascend.map(&:to_s)
443443
end
444444

445445
defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
446446
defassert(:ascend, %w[a/b/c a/b a], "a/b/c")
447447
defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
448448
defassert(:ascend, %w[a/], "a/")
449449

450+
def test_blockless_ascend_is_enumerator
451+
assert_kind_of(Enumerator, Pathname.new('a').ascend)
452+
end
453+
454+
def test_blockless_descend_is_enumerator
455+
assert_kind_of(Enumerator, Pathname.new('a').descend)
456+
end
457+
450458
def test_initialize
451459
p1 = Pathname.new('a')
452460
assert_equal('a', p1.to_s)

0 commit comments

Comments
 (0)