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=, #print, #printf, #puts, read, #rewind, #ungetbyte

Constructor Details

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

Returns a new instance of File.



4
5
6
7
8
9
10
11
12
# File 'mrbgems/mruby-io/mrblib/file.rb', line 4

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

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.directory?(file) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'mrbgems/mruby-io/mrblib/file.rb', line 87

def self.directory?(file)
  FileTest.directory?(file)
end

.exist?(file) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'mrbgems/mruby-io/mrblib/file.rb', line 91

def self.exist?(file)
  FileTest.exist?(file)
end

.exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'mrbgems/mruby-io/mrblib/file.rb', line 95

def self.exists?(file)
  FileTest.exists?(file)
end

.extname(filename) ⇒ Object



127
128
129
130
131
132
# File 'mrbgems/mruby-io/mrblib/file.rb', line 127

def self.extname(filename)
  fname = self.basename(filename)
  epos = fname.rindex('.')
  return '' if epos == 0 || epos.nil?
  return fname[epos..-1]
end

.file?(file) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'mrbgems/mruby-io/mrblib/file.rb', line 99

def self.file?(file)
  FileTest.file?(file)
end

.foreach(file) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'mrbgems/mruby-io/mrblib/file.rb', line 77

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

.join(*names) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'mrbgems/mruby-io/mrblib/file.rb', line 33

def self.join(*names)
  return "" if names.empty?

  names.map! do |name|
    case name
    when String
      name
    when Array
      if names == name
        raise ArgumentError, "recursive array"
      end
      join(*name)
    else
      raise TypeError, "no implicit conversion of #{name.class} into String"
    end
  end

  return names[0] if names.size == 1

  if names[0][-1] == File::SEPARATOR
    s = names[0][0..-2]
  else
    s = names[0].dup
  end

  (1..names.size-2).each { |i|
    t = names[i]
    if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
      t = t[1..-2]
    elsif t[0] == File::SEPARATOR
      t = t[1..-1]
    elsif t[-1] == File::SEPARATOR
      t = t[0..-2]
    end
    s += File::SEPARATOR + t if t != ""
  }
  if names[-1][0] == File::SEPARATOR
    s += File::SEPARATOR + names[-1][1..-1]
  else
    s += File::SEPARATOR + names[-1]
  end
  s
end

.path(filename) ⇒ Object



134
135
136
137
138
139
140
# File 'mrbgems/mruby-io/mrblib/file.rb', line 134

def self.path(filename)
  if filename.kind_of?(String)
    filename
  else
    raise TypeError, "no implicit conversion of #{filename.class} into String"
  end
end

.pipe?(file) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'mrbgems/mruby-io/mrblib/file.rb', line 103

def self.pipe?(file)
  FileTest.pipe?(file)
end

.size(file) ⇒ Object



107
108
109
# File 'mrbgems/mruby-io/mrblib/file.rb', line 107

def self.size(file)
  FileTest.size(file)
end

.size?(file) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'mrbgems/mruby-io/mrblib/file.rb', line 111

def self.size?(file)
  FileTest.size?(file)
end

.socket?(file) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'mrbgems/mruby-io/mrblib/file.rb', line 115

def self.socket?(file)
  FileTest.socket?(file)
end

.symlink?(file) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'mrbgems/mruby-io/mrblib/file.rb', line 119

def self.symlink?(file)
  FileTest.symlink?(file)
end

.zero?(file) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'mrbgems/mruby-io/mrblib/file.rb', line 123

def self.zero?(file)
  FileTest.zero?(file)
end

Instance Method Details

#atimeObject



14
15
16
17
# File 'mrbgems/mruby-io/mrblib/file.rb', line 14

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

#ctimeObject



19
20
21
22
# File 'mrbgems/mruby-io/mrblib/file.rb', line 19

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

#inspectObject



29
30
31
# File 'mrbgems/mruby-io/mrblib/file.rb', line 29

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

#mtimeObject



24
25
26
27
# File 'mrbgems/mruby-io/mrblib/file.rb', line 24

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