summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPaolo Bosetti <[email protected]>2014-03-04 10:43:46 +0100
committerPaolo Bosetti <[email protected]>2014-03-04 10:43:46 +0100
commit7ff049457f5c0c29ce3aec9414b98ae4226d2377 (patch)
tree3ffcde2ff1f4fc5f94975fd73ffe61fddd352e6d
parentd9f3fd868a1c4778dbd7119a00c79ef020dc6048 (diff)
downloadmruby-7ff049457f5c0c29ce3aec9414b98ae4226d2377.tar.gz
mruby-7ff049457f5c0c29ce3aec9414b98ae4226d2377.zip
Added File.foreach method.
Improved error management in File.new (Errno can be undefined)
-rw-r--r--mrblib/file.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/mrblib/file.rb b/mrblib/file.rb
index 4087593a5..9a4333c64 100644
--- a/mrblib/file.rb
+++ b/mrblib/file.rb
@@ -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