Created
October 14, 2010 07:48
-
-
Save jkeefe/625809 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'sinatra' | |
@ap_scratch = [] | |
@county_info = Hash.new | |
@t = "" | |
get '/x' do | |
# Open the Race file | |
File.open('NY_Race.txt').each do |line| # Step thru each line | |
@ap_scratch << line # I think this places each line as an element of this array | |
end | |
@ap_scratch.each do |line| # Step through lines and do the indented set below | |
h = {} | |
results = line.scan(/([^;]*[^;])/).flatten # Regex: extract any number of characters between ;'s that aren't semicolons | |
# Then flatten each array of arrays into just an array | |
# Here we only build onto @county_info only when results[2] == "39760" (Race = New York governor's race) | |
if results[2] == "39760" | |
# Here we build the @county_info hash with fipscode as the key and style as the value | |
# results[6] is the line's fips code, | |
# results[7] is the county name | |
# results[15] is precincts reporting | |
# results[16] is total precincts | |
# this makes the key the county name, and the value "st0"-"st10" depending on no. of prects reporting | |
@county_info[results[7]] = "st" + ((results[15].to_f / results[16].to_f * 10.0).to_i.to_s) | |
end # endif | |
end # end .each | |
@t = erb :New_York_Map | |
p @t | |
end |
I'd just add that the above code -- from lines 1-33 (minus line 8) work fine when I "include" it from irb.
I haven't tried out the code, but my guess is that the instance variables fall out of scope within the Sinatra get
block. Try either defining the instance variables within the block, or changing variables you want to be able to access globally to constants (i.e. @ap_scratch
becomes AP_SCRATCH
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I've been working on this code all evening, and suddenly my instance variables throw "undefined method `<<' for nil:NilClass" errors (that one's from line 12). I have no idea what I've done to cause this. Any idea? The only thing I did was update my rubygems to the current version -- tho it worked for a while after that.