Skip to content

Instantly share code, notes, and snippets.

@dcluna
Created March 20, 2012 17:18
Show Gist options
  • Select an option

  • Save dcluna/2138321 to your computer and use it in GitHub Desktop.

Select an option

Save dcluna/2138321 to your computer and use it in GitHub Desktop.
Metaprog CSV
require 'csv' # ruby 1.9
class FromCSV
def initialize(file)
col_names = nil
CSV.open(file, "r") { |csv_file| col_names = csv_file.shift } # reads first line of the CSV file
@file = file
class <<self
self
end.class_eval do
col_names.each do |col_name|
attr_accessor col_name
end
attr_reader :file
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment