Skip to content

Commit

Permalink
Fix missing Dir#each to be an Enumerable (crystal-lang#5458)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardiff committed Dec 28, 2017
1 parent 4313e86 commit 12cc7f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions spec/std/dir_spec.cr
Expand Up @@ -234,7 +234,7 @@ describe "Dir" do
filenames = [] of String

dir = Dir.new(__DIR__)
dir.each_entry do |filename|
dir.each do |filename|
filenames << filename
end.should be_nil
dir.close
Expand All @@ -246,7 +246,7 @@ describe "Dir" do
filenames = [] of String

Dir.open(__DIR__) do |dir|
dir.each_entry do |filename|
dir.each do |filename|
filenames << filename
end.should be_nil
end
Expand All @@ -272,7 +272,7 @@ describe "Dir" do
it "gets dir iterator" do
filenames = [] of String

iter = Dir.new(__DIR__).each_entry
iter = Dir.new(__DIR__).each
iter.each do |filename|
filenames << filename
end
Expand Down
16 changes: 8 additions & 8 deletions src/dir.cr
Expand Up @@ -48,7 +48,7 @@ class Dir
# File.write("testdir/config.h", "")
#
# d = Dir.new("testdir")
# d.each_entry { |x| puts "Got #{x}" }
# d.each { |x| puts "Got #{x}" }
# ```
#
# produces:
Expand All @@ -58,20 +58,20 @@ class Dir
# Got ..
# Got config.h
# ```
def each_entry : Nil
def each : Nil
while entry = read
yield entry
end
end

def each_entry
def each
EntryIterator.new(self)
end

# Returns an array containing all of the filenames in the given directory.
def entries : Array(String)
entries = [] of String
each_entry do |filename|
each do |filename|
entries << filename
end
entries
Expand All @@ -85,7 +85,7 @@ class Dir
# File.write("testdir/config.h", "")
#
# d = Dir.new("testdir")
# d.each_entry { |x| puts "Got #{x}" }
# d.each_child { |x| puts "Got #{x}" }
# ```
#
# produces:
Expand Down Expand Up @@ -181,10 +181,10 @@ class Dir
end
end

# See `#each_entry`.
def self.each_entry(dirname)
# See `#each`.
def self.each(dirname)
Dir.open(dirname) do |dir|
dir.each_entry do |filename|
dir.each do |filename|
yield filename
end
end
Expand Down

0 comments on commit 12cc7f2

Please sign in to comment.