Created
March 20, 2012 17:18
-
-
Save dcluna/2138321 to your computer and use it in GitHub Desktop.
Metaprog CSV
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 '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