Skip to content

Commit

Permalink
Implemented Dir.children (crystal-lang#4808)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and Val committed Aug 12, 2017
1 parent 79d479a commit d55b618
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit d55b618

Please sign in to comment.