Skip to content
Permalink
Browse files

Merge pull request #806 from kostya/dir

Dir#open,foreach,path and refactor coercion
  • Loading branch information
alex committed Jul 14, 2013
2 parents 5593987 + e176aac commit a66c520e9b8c0496089d5655c092ee7e0fa25c88
@@ -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
@@ -32,15 +32,9 @@ def self.size(filename)
end

def self.zero?(filename)
if filename.respond_to?(:to_path)
filename = filename.to_path
end

begin
File.size(filename) == 0
rescue Errno::ENOENT
false
end
File.size(filename) == 0
rescue Errno::ENOENT
false
end

def size
@@ -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.

This file was deleted.

This file was deleted.

@@ -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.

@@ -26,7 +26,7 @@ def ensure_open(self, space):
if not self.open:
raise space.error(space.w_IOError, "closed directory")

@classdef.method("initialize", path="str")
@classdef.method("initialize", path="path")
def method_initialize(self, space, path):
self.path = path
try:
@@ -127,6 +127,10 @@ def method_close(self, space):
self.open = False
return space.w_nil

@classdef.method("path")
def method_path(self, space):
return space.newstr_fromstr(self.path)

@classdef.singleton_method("mkdir", path="path", mode="int")
def method_mkdir(self, space, path, mode=0777):
try:

0 comments on commit a66c520

Please sign in to comment.
You can’t perform that action at this time.