Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/file.rb
Constant Summary collapse
- @@vfile =
{}
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #clear ⇒ Object
- #close ⇒ Object
- #each_line(rs = "", limit = 0) ⇒ Object
- #getc ⇒ Object
- #gets ⇒ Object
-
#initialize(path) ⇒ File
constructor
A new instance of File.
- #read(length = nil, outbuf = '') ⇒ Object
- #read_nonblock(maxlen, outbuf = '') ⇒ Object
- #readchar ⇒ Object
- #readline ⇒ Object
- #readlines(rs = '', limit = 0) ⇒ Object
- #readpartial(maxlen, outbuf = '') ⇒ Object
- #sysread(maxlen, outbuf = '') ⇒ Object
- #to_s ⇒ Object
- #ungetc(ch) ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/file.rb', line 7 def initialize(path) if @@vfile.include? path vfile = @@vfile[path] @io = vfile.io @hash = @io.string.hash else @io = StringIO.new '' @hash = ''.hash @@vfile[path] = self end @path = path @buf = [] @old_hash = @hash - 1 end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
4 5 6 |
# File 'lib/file.rb', line 4 def io @io end |
Class Method Details
.foreach(path, rs = '', &block) ⇒ Object
121 122 123 |
# File 'lib/file.rb', line 121 def self.foreach(path, rs = '',&block) self.open(path).each_line(block) end |
.open(path, &block) ⇒ Object
125 126 127 128 129 |
# File 'lib/file.rb', line 125 def self.open(path,&block) f = self.new(path) block.call f if block f end |
Instance Method Details
#<<(str) ⇒ Object
22 23 24 |
# File 'lib/file.rb', line 22 def <<(str) self.write(str) end |
#clear ⇒ Object
108 109 110 111 112 |
# File 'lib/file.rb', line 108 def clear @io.close @io = StringIO.new '' self end |
#close ⇒ Object
114 115 116 117 118 119 |
# File 'lib/file.rb', line 114 def close @buf = nil @hash = nil @old_hash = nil @path = nil end |
#each_line(rs = "", limit = 0) ⇒ Object
36 37 38 39 40 |
# File 'lib/file.rb', line 36 def each_line(rs = "",limit = 0) @io.string.split("\n").each do |x| yield x end end |
#getc ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/file.rb', line 42 def getc unless @old_hash == @hash @old_hash = @hash @buf = self.to_s.split('') end @buf.shift end |
#gets ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/file.rb', line 50 def gets buf = '' ch = '' while (ch = self.getc) != "\n" && ch buf << ch end buf << ch.to_s end |
#read(length = nil, outbuf = '') ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/file.rb', line 63 def read(length = nil,outbuf = '') buf = '' if length (0..length).each do buf << self.getc end outbuf.sub!(/^.#{length}/,buf) else outbuf.gsub!(/./,'') outbuf << self.readlines.join('') end buf end |
#read_nonblock(maxlen, outbuf = '') ⇒ Object
77 78 79 |
# File 'lib/file.rb', line 77 def read_nonblock(maxlen, outbuf = '') read(maxlen,outbuf) end |
#readchar ⇒ Object
89 90 91 92 |
# File 'lib/file.rb', line 89 def readchar ch = self.getc raise EOFError unless ch end |
#readline ⇒ Object
94 95 96 97 98 |
# File 'lib/file.rb', line 94 def readline line = self.gets raise EOFError unless line line end |
#readlines(rs = '', limit = 0) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/file.rb', line 100 def readlines(rs = '',limit = 0) lines = [] self.each_line(rs,limit) do |l| lines << l end lines end |
#readpartial(maxlen, outbuf = '') ⇒ Object
81 82 83 |
# File 'lib/file.rb', line 81 def readpartial(maxlen, outbuf = '') read(maxlen,outbuf) end |
#sysread(maxlen, outbuf = '') ⇒ Object
85 86 87 |
# File 'lib/file.rb', line 85 def sysread(maxlen, outbuf = '') read(maxlen,outbuf) end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/file.rb', line 26 def to_s @io.string end |
#ungetc(ch) ⇒ Object
59 60 61 |
# File 'lib/file.rb', line 59 def ungetc(ch) @buf.unshift(ch) end |
#write(str) ⇒ Object
30 31 32 33 34 |
# File 'lib/file.rb', line 30 def write(str) c = @io.write(str) @hash = self.to_s.hash c end |