Class: File

Inherits:
IO
  • Object
show all
Includes:
Constants
Defined in:
mrbgems/mruby-io/mrblib/file.rb,
mrbgems/mruby-io/mrblib/file_constants.rb,
mrbgems/mruby-io/mrblib/file_constants.rb

Defined Under Namespace

Modules: Constants

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IO

#each, #each_byte, #each_char, #hash, open, pipe, popen, #pos=, #printf, read, #rewind

Constructor Details

#initialize(fd_or_path, mode = "r", perm = 0666) ⇒ File

call-seq:

File.new(filename, mode="r")            -> file
File.new(filename [, mode [, perm]])    -> file
File.new(fd [, mode])                   -> file

Opens the file named by filename according to the given mode and returns a new File object. If a file descriptor is given instead of a filename, the new File object will be associated with that descriptor.

f = File.new("testfile", "r")
f = File.new("newfile",  "w+")
f = File.new("tmpfile",  "a")


19
20
21
22
23
24
25
26
27
# File 'mrbgems/mruby-io/mrblib/file.rb', line 19

def initialize(fd_or_path, mode = "r", perm = 0666)
  if fd_or_path.kind_of? Integer
    super(fd_or_path, mode)
  else
    @path = fd_or_path
    fd = IO.sysopen(@path, mode, perm)
    super(fd, mode)
  end
end

Instance Attribute Details

#pathObject

The path to the file



3
4
5
# File 'mrbgems/mruby-io/mrblib/file.rb', line 3

def path
  @path
end

Class Method Details

.foreach(file) ⇒ Object

call-seq:

File.foreach(name) {|line| block }  -> nil
File.foreach(name)                  -> an_enumerator

Executes the block for every line in the named I/O port, where lines are separated by sep.

File.foreach("testfile") {|x| print "GOT ", x }
GOT This is line one
GOT This is line two
GOT This is line three
GOT And so on...


97
98
99
100
101
102
103
104
105
# File 'mrbgems/mruby-io/mrblib/file.rb', line 97

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

Instance Method Details

#atimeObject

call-seq:

file.atime    -> time

Returns the last access time for file, or epoch if the platform doesn't have access time.

File.new("testfile").atime   #=> Wed Apr 09 08:51:48 CDT 2003


38
39
40
41
# File 'mrbgems/mruby-io/mrblib/file.rb', line 38

def atime
  t = self._atime
  t && Time.at(t)
end

#ctimeObject

call-seq:

file.ctime -> time

Returns the change time for file (that is, the time directory information about the file was changed, not the file itself).

File.new("testfile").ctime   #=> Wed Apr 09 08:53:13 CDT 2003


52
53
54
55
# File 'mrbgems/mruby-io/mrblib/file.rb', line 52

def ctime
  t = self._ctime
  t && Time.at(t)
end

#inspectObject

call-seq:

file.inspect  -> string

Return a string describing this File object.

File.new("testfile").inspect   #=> "#<File:testfile>"


78
79
80
# File 'mrbgems/mruby-io/mrblib/file.rb', line 78

def inspect
  "<#{self.class}:#{@path}>"
end

#mtimeObject

call-seq:

file.mtime -> time

Returns the modification time for file.

File.new("testfile").mtime   #=> Wed Apr 09 08:53:14 CDT 2003


65
66
67
68
# File 'mrbgems/mruby-io/mrblib/file.rb', line 65

def mtime
  t = self._mtime
  t && Time.at(t)
end