From 7ff049457f5c0c29ce3aec9414b98ae4226d2377 Mon Sep 17 00:00:00 2001 From: Paolo Bosetti Date: Tue, 4 Mar 2014 10:43:46 +0100 Subject: Added File.foreach method. Improved error management in File.new (Errno can be undefined) --- mrblib/file.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'mrblib/file.rb') 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 -- cgit v1.2.3 From 39bd240116b2980f90f9d8f350e18311959db54b Mon Sep 17 00:00:00 2001 From: Paolo Bosetti Date: Fri, 7 Mar 2014 16:47:18 +0100 Subject: Fixed bug in File.expand_path() on Windows (forever recursive loop) --- mrblib/file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrblib/file.rb') diff --git a/mrblib/file.rb b/mrblib/file.rb index 9a4333c64..a4fcd3212 100644 --- a/mrblib/file.rb +++ b/mrblib/file.rb @@ -64,7 +64,7 @@ class File < IO def self.expand_path(path, default_dir = '.') def concat_path(path, base_path) - if path[0] == "/" + if path[0] == "/" || path[1] == ':' # Windows root! expanded_path = path elsif path[0] == "~" if (path[1] == "/" || path[1] == nil) -- cgit v1.2.3