Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing Dir#each to be an Enumerable #5458

Merged
merged 1 commit into from
Dec 28, 2017
Merged
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
6 changes: 3 additions & 3 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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