Skip to content

Instantly share code, notes, and snippets.

@alexec
Created August 3, 2013 10:33
Show Gist options
  • Save alexec/6146037 to your computer and use it in GitHub Desktop.
Save alexec/6146037 to your computer and use it in GitHub Desktop.
A class to keep a short, persistent history of some values.
require 'csv'
class History
def initialize(name, cols, size)
@f=name
@headers=cols
@size=size
read
write
end
def read
@csv=[]
if File.exists?(@f)
CSV.foreach(@f, {:headers => :first_row, :converters => [:numeric]}) do |row|
@csv << row
end
end
@csv=@csv.last(@size)
end
def write
@csv=@csv.last(@size)
CSV.open(@f, 'w') do |out|
out << @headers
@csv.each{|row| out << row}
end
end
def << (row)
@csv << row
write
end
private :read, :write
end
History.new('hist.csv', ['x','y'], 5) << [rand(),rand()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment