Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created May 12, 2012 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jared-Prime/2664038 to your computer and use it in GitHub Desktop.
Save Jared-Prime/2664038 to your computer and use it in GitHub Desktop.
survey reader for Think Stats
# finally, correctly rewrote the Python script "survey" in Think Stats to Ruby.
# burned the midnight candle to get this right. feels great to succeed
class Table < Hash
def initialize(name=nil)
self[:records] = []
self[:name] = name
end
def length
return self[:records].size
end
def add(record)
self[:records] << record
end
def expand(records)
for record in records
self.add(record)
end
end
# Here's where the fun begins
def upload(filename, fields)
file = File.open(filename, "r")
file.each_line { |line|
self.generate(line, fields)
}
end
def generate(line, fields={})
record = {}
fields.each { |key, value|
record[key] = line[value].to_i
}
self.add(record)
end
def recode
# not yet implemented
end
end
# Copyleft 2012, Jared Davis @haiqus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment