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

Implemented Dir.children #4808

Merged
merged 1 commit into from Aug 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions spec/std/dir_spec.cr
Expand Up @@ -256,9 +256,15 @@ describe "Dir" do

it "lists entries" do
filenames = Dir.entries(__DIR__)
filenames.includes?(".").should be_true
filenames.includes?("..").should be_true
filenames.includes?("dir_spec.cr").should be_true
end

it "lists children" do
Dir.children(__DIR__).should eq(Dir.entries(__DIR__) - %w(. ..))
end

it "does to_s" do
Dir.new(__DIR__).to_s.should eq("#<Dir:#{__DIR__}>")
end
Expand Down
11 changes: 11 additions & 0 deletions src/dir.cr
Expand Up @@ -154,6 +154,17 @@ class Dir
entries
end

# Returns an array containing all of the filenames except for `.` and `..`
# in the given directory.
def self.children(dirname) : Array(String)
excluded = {".", ".."}
entries = [] of String
foreach(dirname) do |filename|
entries << filename unless excluded.includes?(filename)
end
entries
end

# Returns `true` if the given path exists and is a directory
def self.exists?(path) : Bool
if LibC.stat(path.check_no_null_byte, out stat) != 0
Expand Down