Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ New features:
Bug fixes:

* Add missing thread-safe objects write barriers for `TruffleRuby::ConcurrentMap` (#3179, @eregon).
* Fix repeated calling of methods `Dir#{each,each_child,children}` (#3464, @andrykonchin).

Compatibility:

Expand Down
13 changes: 13 additions & 0 deletions spec/ruby/core/dir/children_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,17 @@
children = @dir.children.sort
children.first.encoding.should equal(Encoding::EUC_KR)
end

it "returns the same result when called repeatedly" do
@dir = Dir.open DirSpecs.mock_dir

a = []
@dir.each {|dir| a << dir}

b = []
@dir.each {|dir| b << dir}

a.sort.should == b.sort
a.sort.should == DirSpecs.expected_paths
end
end
13 changes: 13 additions & 0 deletions spec/ruby/core/dir/each_child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@
@dir.each_child { |f| f }.should == @dir
end

it "returns the same result when called repeatedly" do
@dir = Dir.open DirSpecs.mock_dir

a = []
@dir.each {|dir| a << dir}

b = []
@dir.each {|dir| b << dir}

a.sort.should == b.sort
a.sort.should == DirSpecs.expected_paths
end

describe "when no block is given" do
it "returns an Enumerator" do
@dir = Dir.new(DirSpecs.mock_dir)
Expand Down
11 changes: 11 additions & 0 deletions spec/ruby/core/dir/each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
ls.should include(@dir.read)
end

it "returns the same result when called repeatedly" do
a = []
@dir.each {|dir| a << dir}

b = []
@dir.each {|dir| b << dir}

a.sort.should == b.sort
a.sort.should == DirSpecs.expected_paths
end

describe "when no block is given" do
it "returns an Enumerator" do
@dir.each.should be_an_instance_of(Enumerator)
Expand Down
6 changes: 6 additions & 0 deletions src/main/ruby/truffleruby/core/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def closed?
def each
return to_enum unless block_given?

rewind

while s = read
yield s
end
Expand All @@ -124,6 +126,8 @@ def each
end

def children
rewind

ret = []
while s = read
ret << s if s != '.' and s != '..'
Expand All @@ -134,6 +138,8 @@ def children
def each_child
return to_enum(:each_child) unless block_given?

rewind

while s = read
yield s unless s == '.' or s == '..'
end
Expand Down