Browse files

JRUBY-217: Dir#glob doesn't handle blocks correctly (Ryan Bell)

git-svn-id: http://svn.codehaus.org/jruby/trunk@2370 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent 874d5f3 commit cf928201f66dd073dc2e4206ea28c4ec50169ff6 @enebo enebo committed Oct 22, 2006
Showing with 22 additions and 2 deletions.
  1. +11 −2 jruby/src/org/jruby/RubyDir.java
  2. +11 −0 jruby/test/testDir.rb
View
13 jruby/src/org/jruby/RubyDir.java
@@ -138,11 +138,20 @@ public IRubyObject initialize(RubyString newPath) {
/**
* Returns an array of filenames matching the specified wildcard pattern
- * <code>pat</code>.
+ * <code>pat</code>. If a block is given, the array is iterated internally
+ * with each filename is passed to the block in turn. In this case, Nil is
+ * returned.
*/
- public static RubyArray glob(IRubyObject recv, RubyString pat) {
+ public static IRubyObject glob(IRubyObject recv, RubyString pat) {
String pattern = pat.toString();
String[] files = new Glob(recv.getRuntime().getCurrentDirectory(), pattern).getNames();
+ ThreadContext context = recv.getRuntime().getCurrentContext();
+ if (context.isBlockGiven()) {
+ for (int i = 0; i < files.length; i++) {
+ context.yield(JavaUtil.convertJavaToRuby(recv.getRuntime(), files[i]));
+ }
+ return recv.getRuntime().getNil();
+ }
return recv.getRuntime().newArray(JavaUtil.convertJavaArrayToRuby(recv.getRuntime(), files));
}
View
11 jruby/test/testDir.rb
@@ -52,6 +52,17 @@
File.delete("testDir_2/testDir_tmp1")
Dir.delete("testDir_2")
+# Test that glob handles blocks correctly
+Dir.mkdir("testDir_3")
+open("testDir_3/testDir_tmp1", "w").close
+vals = []
+glob_val = Dir.glob('**/*tmp1'){|f| vals << f}
+test_equal(true, glob_val.nil?)
+test_equal(1, vals.size)
+test_equal(true, File.exists?(vals[0])) unless vals.empty?
+File.delete("testDir_3/testDir_tmp1")
+Dir.delete("testDir_3")
+
# begin JIRA 31 issues
# works with MRI though not JRuby ('.' gets globbed)

0 comments on commit cf92820

Please sign in to comment.