From 7ebbe2a909b38527bccb0d791506e3d64401dcb0 Mon Sep 17 00:00:00 2001 From: Kazuki Tsujimoto Date: Thu, 3 Nov 2011 22:51:29 +0900 Subject: [PATCH] * lib/pathname.rb (Pathname#find): return an enumerator if no block is given. * test/pathname/test_pathname.rb: add tests for above. --- ChangeLog | 7 +++++++ NEWS | 4 ++++ ext/pathname/lib/pathname.rb | 3 +++ test/pathname/test_pathname.rb | 4 ++++ 4 files changed, 18 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 273b447..90a1962 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Nov 5 13:49:40 2011 Kazuki Tsujimoto + + * lib/pathname.rb (Pathname#find): return an enumerator if + no block is given. + + * test/pathname/test_pathname.rb: add tests for above. + Sat Nov 5 11:18:12 2011 Tanaka Akira * ext/socket/socket.c (rsock_socketpair0): don't clear diff --git a/NEWS b/NEWS index b52a7d1..f663b29 100644 --- a/NEWS +++ b/NEWS @@ -40,6 +40,10 @@ with all sufficient information, see the ChangeLog file. * Net::IMAP.default_ssl_port * Net::IMAP.default_imaps_port +* pathname + * extended method: + * Pathname#find returns an enumerator if no block is given. + * resolv * new methods: * Resolv::DNS#timeouts= diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index 1d2b37c..1cfca0b 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -489,6 +489,8 @@ class Pathname # * Find * # Pathname#find is an iterator to traverse a directory tree in a depth first # manner. It yields a Pathname for each file under "this" directory. # + # Returns an enumerator if no block is given. + # # Since it is implemented by find.rb, Find.prune can be used # to control the traversal. # @@ -496,6 +498,7 @@ class Pathname # * Find * # current directory, not ./. # def find(&block) # :yield: pathname + return to_enum(__method__) unless block_given? require 'find' if @path == '.' Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) } diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index ac9b415..401bad2 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1235,6 +1235,10 @@ class TestPathname < Test::Unit::TestCase assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a) a = []; Pathname("d").find {|v| a << v }; a.sort! assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a) + a = Pathname(".").find.sort + assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a) + a = Pathname("d").find.sort + assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a) } end -- 1.7.5.4