Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
dir.c: Dir#each_child
* dir.c (dir_each_child_m): new instance methods Dir#each_child
  and Dir#children.  [Feature #13969]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62022 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jan 24, 2018
1 parent f7210de commit 6a3a7e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions NEWS
Expand Up @@ -20,6 +20,12 @@ with all sufficient information, see the ChangeLog file or Redmine

=== Core classes updates (outstanding ones only)

* Dir

* New methods:

* added Dir#each_child and Dir#children instance methods. [Feature #13969]

* Proc

* Proc#call doesn't change $SAFE any more. [Feature #14250]
Expand Down
9 changes: 9 additions & 0 deletions dir.c
Expand Up @@ -2859,6 +2859,13 @@ dir_s_each_child(int argc, VALUE *argv, VALUE io)
return Qnil;
}

static VALUE
dir_each_child_m(VALUE dir)
{
RETURN_ENUMERATOR(dir, 0, 0);
return dir_each_entry(dir, dir_yield, Qnil, TRUE);
}

static VALUE
dir_collect_children(VALUE dir)
{
Expand Down Expand Up @@ -3212,6 +3219,8 @@ Init_Dir(void)
rb_define_method(rb_cDir,"inspect", dir_inspect, 0);
rb_define_method(rb_cDir,"read", dir_read, 0);
rb_define_method(rb_cDir,"each", dir_each, 0);
rb_define_method(rb_cDir,"each_child", dir_each_child_m, 0);
rb_define_method(rb_cDir,"children", dir_collect_children, 0);
rb_define_method(rb_cDir,"rewind", dir_rewind, 0);
rb_define_method(rb_cDir,"tell", dir_tell, 0);
rb_define_method(rb_cDir,"seek", dir_seek, 1);
Expand Down
3 changes: 3 additions & 0 deletions test/ruby/test_dir.rb
Expand Up @@ -232,14 +232,17 @@ def test_entries
end

def test_foreach
assert_entries(Dir.open(@root) {|dir| dir.each.to_a})
assert_entries(Dir.foreach(@root).to_a)
end

def test_children
assert_entries(Dir.open(@root) {|dir| dir.children}, true)
assert_entries(Dir.children(@root), true)
end

def test_each_child
assert_entries(Dir.open(@root) {|dir| dir.each_child.to_a}, true)
assert_entries(Dir.each_child(@root).to_a, true)
end

Expand Down

0 comments on commit 6a3a7e9

Please sign in to comment.