Created
May 12, 2012 03:57
-
-
Save Jared-Prime/2664038 to your computer and use it in GitHub Desktop.
survey reader for Think Stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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