require 'fastercsv' # ==== Rows # 0 -> month # 1 -> region # 2 -> mday # 3 -> wday # 4 -> week # 5 -> name rules_by_month = {} FasterCSV.foreach('data/us.csv', {:headers => :first_row, :return_headers => false, :force_quotes => true}) do |row| month = row['month'].to_i rules_by_month[month] = [] unless rules_by_month[month] rule = {} row.each do |key, val| rule[key] = val end rules_by_month[month] << rule end out = "# This file is generated by one of the Holiday gem's Rake tasks.\n" out << "HOLIDAYS_BY_MONTH = {\n" month_strs = [] rules_by_month.each do |month, rules| month_str = " #{month.to_s} => [" rule_strings = [] rules.each do |rule| str = '{' if rule['mday'] str << ":mday => #{rule['mday']}, " else str << ":wday => #{rule['wday']}, :week => #{rule['week']}, " end str << ":name => \"#{rule['name']}\", :regions => [:#{rule['region']}]}" rule_strings << str end month_str << rule_strings.join(",\n ") + "]" month_strs << month_str end month_strs.join(",\n") out << month_strs.join(",\n") + "\n}" File.open("test_file.rb","w") do |file| file.puts out end