Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 153f62356a0a
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 19a43e1ea40c
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 5, 2018

  1. Copy the full SHA
    b7d5c50 View commit details

Commits on Jan 8, 2018

  1. Implement Dir.each_child

    For more information, please see feature #11302.
    nomadium committed Jan 8, 2018
    Copy the full SHA
    c4711e3 View commit details

Commits on Jan 10, 2018

  1. Merge pull request #4953 from nomadium/implement-dir-each-child

    Implement Dir.each_child
    enebo authored Jan 10, 2018
    Copy the full SHA
    19a43e1 View commit details
Showing with 76 additions and 0 deletions.
  1. +72 −0 core/src/main/java/org/jruby/RubyDir.java
  2. +4 −0 test/mri/ruby/test_dir.rb
72 changes: 72 additions & 0 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -397,6 +397,37 @@ private static IRubyObject rmdirCommon(Ruby runtime, String path) {
return runtime.newFixnum(0);
}

/**
* Executes the block once for each entry in the directory except
* for "." and "..", passing the filename of each entry as parameter
* to the block.
*/
@JRubyMethod(name = "each_child", meta = true)
public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IRubyObject arg, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);

return eachChildCommon(context, recv, context.runtime, pathString, null, block);
}

/**
* Executes the block once for each entry in the directory except
* for "." and "..", passing the filename of each entry as parameter
* to the block.
*/
@JRubyMethod(name = "each_child", meta = true)
public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject enc, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);
RubyEncoding encoding;

if (enc instanceof RubyEncoding) {
encoding = (RubyEncoding) enc;
} else {
throw context.runtime.newTypeError(enc, context.runtime.getEncoding());
}

return eachChildCommon(context, recv, context.runtime, pathString, encoding, block);
}

/**
* Executes the block once for each file in the directory specified by
* <code>path</code>.
@@ -426,6 +457,22 @@ public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRu
return foreachCommon(context, recv, context.runtime, pathString, encoding, block);
}

private static IRubyObject eachChildCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, RubyEncoding encoding, Block block) {
if (block.isGiven()) {
RubyClass dirClass = runtime.getDir();
RubyDir dir = (RubyDir) dirClass.newInstance(context, new IRubyObject[]{_path}, block);

dir.each_child(context, block);
return runtime.getNil();
}

if (encoding == null) {
return enumeratorize(runtime, recv, "each_child", _path);
} else {
return enumeratorize(runtime, recv, "each_child", new IRubyObject[]{_path, encoding});
}
}

private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, RubyEncoding encoding, Block block) {
if (block.isGiven()) {
RubyClass dirClass = runtime.getDir();
@@ -561,13 +608,38 @@ public IRubyObject each(ThreadContext context, Encoding enc, Block block) {
return this;
}

/**
* Executes the block once for each child in the directory
* (i.e. all the directory entries except for "." and "..").
*/
public IRubyObject each_child(ThreadContext context, Encoding enc, Block block) {
checkDir();

String[] contents = snapshot;
for (pos = 0; pos < contents.length; pos++) {
if (!(contents[pos].equals(".") || contents[pos].equals(".."))) {
block.yield(context, RubyString.newString(context.runtime, contents[pos], enc));
}
}

return this;
}

/**
* Executes the block once for each entry in the directory.
*/
public IRubyObject each(ThreadContext context, Block block) {
return each(context, context.runtime.getDefaultInternalEncoding(), block);
}

/**
* Executes the block once for each child in the directory
* (i.e. all the directory entries except for "." and "..").
*/
public IRubyObject each_child(ThreadContext context, Block block) {
return each_child(context, context.runtime.getDefaultInternalEncoding(), block);
}

@JRubyMethod(name = "each")
public IRubyObject each19(ThreadContext context, Block block) {
return block.isGiven() ? each(context, block) : enumeratorize(context.runtime, this, "each");
4 changes: 4 additions & 0 deletions test/mri/ruby/test_dir.rb
Original file line number Diff line number Diff line change
@@ -204,6 +204,10 @@ def test_children
assert_entries(Dir.children(@root), true)
end

def test_each_child
assert_entries(Dir.each_child(@root).to_a, true)
end

def test_dir_enc
dir = Dir.open(@root, encoding: "UTF-8")
begin