Skip to content

Instantly share code, notes, and snippets.

@ryanjm
Created June 29, 2012 23:32
Show Gist options
  • Save ryanjm/3021405 to your computer and use it in GitHub Desktop.
Save ryanjm/3021405 to your computer and use it in GitHub Desktop.
# opens file
f = File.new('small.csv','r')
# grab all the lines (array)
lines = f.readlines
# header will be first line, take it out and strip white space
headers = lines.shift.strip
keys = headers.split(',')
# value to hold presidents
presidents = []
# loop over remaining lines
lines.each do |line|
values = line.strip.split(',')
params = {}
keys.each_with_index do |key,i|
params[key] = values[i]
end
presidents << params
end
puts "Presidents:"
puts presidents
Presidency President
1 George Washington
2 John Adams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment