mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Added File.foreach method.
Improved error management in File.new (Errno can be undefined)
This commit is contained in:
@@ -12,9 +12,17 @@ class File < IO
|
||||
if fd_or_path.kind_of? Fixnum
|
||||
super(fd_or_path, mode)
|
||||
else
|
||||
if Object.const_defined? :Errno
|
||||
eclass = [Errno::ENOENT, Errno::ENFILE]
|
||||
else
|
||||
eclass = FileError
|
||||
end
|
||||
|
||||
@path = fd_or_path
|
||||
begin
|
||||
fd = IO.sysopen(@path, mode, perm)
|
||||
rescue RuntimeError => e
|
||||
raise FileError, "Could not open file (#{e})"
|
||||
rescue Errno::EMFILE, Errno::ENFILE
|
||||
GC.start
|
||||
fd = IO.sysopen(@path, mode, perm)
|
||||
@@ -119,6 +127,16 @@ class File < IO
|
||||
end
|
||||
end
|
||||
|
||||
def self.foreach(file)
|
||||
if block_given?
|
||||
self.open(file) do |f|
|
||||
f.each {|l| yield l}
|
||||
end
|
||||
else
|
||||
return self.new(file)
|
||||
end
|
||||
end
|
||||
|
||||
def self.directory?(file)
|
||||
FileTest.directory?(file)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user