Class: RandomFile

Inherits:
Object
  • Object
show all
Defined in:
lib/random_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, recordsize = 80) ⇒ RandomFile

Returns a new instance of RandomFile.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/random_file.rb', line 58

def initialize(filename = nil,recordsize = 80)
  if filename == nil
     filename = File.join(Rails.root, 'log', 'NoNameR-'+Time.now.localtime.strftime("%Y-%m-%d")+".log")
  end
  @recordsize = recordsize
  if FileTest::exist?(filename)
    @file = File.new(filename, 'r+')
  else
    @file = File.new(filename, 'w+')
  end
end

Instance Method Details

#appendObject



89
90
91
# File 'lib/random_file.rb', line 89

def append
  @file.seek(0,IO::SEEK_END)
end

#closeObject



93
94
95
# File 'lib/random_file.rb', line 93

def close
  @file.close
end

#eofObject



70
71
72
# File 'lib/random_file.rb', line 70

def eof
  return @file.eof? 
end

#posObject



74
75
76
# File 'lib/random_file.rb', line 74

def pos
  return @file.pos / @recordsize
end

#readObject



97
98
99
100
101
102
103
104
# File 'lib/random_file.rb', line 97

def read
  res = @file.read(@recordsize)
  posn = res.index(0.chr)
  if posn != nil
    res = res[0,posn]
  end
  return res
end

#seek(posn) ⇒ Object



85
86
87
# File 'lib/random_file.rb', line 85

def seek(posn)
  @file.seek(@recordsize * posn)
end

#write(val) ⇒ Object



78
79
80
81
82
83
# File 'lib/random_file.rb', line 78

def write(val)
  while val.length < @recordsize
    val = val + 0.chr
  end
  @file.write val
end