-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from kostya/dir
Dir#open,foreach,path and refactor coercion
- Loading branch information
Showing
9 changed files
with
40 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,40 @@ | ||
class Dir | ||
def self.exists?(dirname) | ||
if dirname.respond_to?(:to_path) | ||
dirname = dirname.to_path | ||
end | ||
begin | ||
!!new(dirname) | ||
rescue Errno::ENOENT, Errno::ENOTDIR | ||
false | ||
end | ||
!!new(dirname) | ||
rescue Errno::ENOENT, Errno::ENOTDIR | ||
false | ||
end | ||
|
||
class << self | ||
alias :exist? :exists? | ||
end | ||
|
||
def self.open(path, opts = nil, &block) | ||
dir = new(path) | ||
if block | ||
value = nil | ||
|
||
begin | ||
value = yield dir | ||
ensure | ||
dir.close | ||
end | ||
|
||
value | ||
else | ||
dir | ||
end | ||
end | ||
|
||
def self.foreach(path, &block) | ||
return self.enum_for(:foreach, path) unless block | ||
|
||
open(path) do |dir| | ||
while s = dir.read | ||
yield s | ||
end | ||
end | ||
|
||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
fails:Dir#close closes the stream and fd and returns nil | ||
fails:Dir#close raises an IOError when called on a closed Dir instance |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
fails:Dir#path returns the path that was supplied to .new or .open | ||
fails:Dir#path returns the path even when called on a closed Dir instance | ||
fails:Dir#path returns a String with the same encoding as the argument to .open |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters