JumpStart Labs事件管理器:语法错误,意外':'

时间:2013-05-01 19:43:29

标签: ruby syntax

我正在研究JumpStart Labs事件管理器,特别是本周定位的时间/天,我遇到了麻烦。当我通过终端运行以下代码时,它给我以下错误[编辑]:

austin-winslows-macbook-4:event_manager HypnoBoy$ ruby event_manager.rb
event_manager.rb:8: odd number list for Hash
...vent_attendees.csv', {headers: true, header_converters: :sym...
                          ^
event_manager.rb:8: syntax error, unexpected ':', expecting '}'
...vent_attendees.csv', {headers: true, header_converters: :sym...
                          ^
event_manager.rb:8: Can't assign to true
...ttendees.csv', {headers: true, header_converters: :symbol})
                          ^
event_manager.rb:8: syntax error, unexpected ':', expecting '='
...ders: true, header_converters: :symbol})

我在下面发布了我的代码,正在寻找建议!关于语法的一些东西显然是关闭的,但到目前为止我已经遵循了这个字母的步骤,并且没有任何问题,所以我不知道在哪里看。任何帮助都会有很大的帮助,谢谢!

require 'csv'
require 'sunlight/congress'
require 'erb'
require 'date'


Sunglight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5"
contents = CSV.open('event_attendees.csv', {headers: true, header_converters: :symbol})


def clean_zipcode(zipcode)
zipcode.to_s.rjust(5,"0")[0..4]
end


def clean_phone(number)
number.to_s.rjust(10,"0")[0..4] 
end


def legislators_by_zipcode(zipcode)
Sunglight::Congress::Legislator.by_zipcode(zipcode)
end


def peak_days
time = row[:regdate]
day_array = []
time.each { |t|
    array << Datetime.strptime(t, '%m/%d/%Y %H:%M').wday }
end

def peak_hours
time = row[:regdate]
hr_array = []
time.each { |t|
        array << DateTime.strptime(t, '%m/%d/%Y %H:%M').hour }
array
end


def save_thanks_you_letters(id,form_letter)
Dir.mkdir("output") unless Dir.exists? "output"

filename = "output/thanks_#{id}.html"

File.open(filename, 'w') { |file|
    file.puts form_letter} 
end


puts "EventManager Initialized!"

template_letter = File.read "form_letter.erb"
erb_template = ERB.new template_letter

contents.each { |row|
id = row[0]

name = row[:first_name]

zipcode = clean_zipcode(row[:zipcode])

phone = clean_phone(row[:homephone]) 

legislators = legislators_by_zipcode(zipcode)

form_letter = erb_template.result(binding)

save_thank_you_letters(id,form_letter)
}

2 个答案:

答案 0 :(得分:0)

来自您使用构造的文档CSV::open

open( filename, options = Hash.new )

所以你排队:

contents = CSV.open 'event_attendees.csv', headers: true, header_converters: :symbol是错误的,因为从第二个参数开始,它期待Hash。因此将其更改为:

contents = CSV.open('event_attendees.csv', {headers: true, header_converters: :symbol})

答案 1 :(得分:0)

我今天完成了这个练习。我没有必要更改contents = CSV.open行。导致错误的原因是日期未在Excel文件中格式化。我在Excel中将日期列格式化为mm / dd / yyyy hh:mm。同样大写似乎在'%m /%d /%Y%H:%M'字符串中 - 我使用小写'y'。

这是我的第一次练习:

# Iteration: Time Targeting
contents = CSV.open "event_attendees.csv", headers: true, header_converters: :symbol

regtimes = Array.new(25, 0)

contents.each do |row|

  reghour = DateTime.strptime(row[:regdate],'%m/%d/%y %H:%M').hour

  regtimes[reghour] += 1

end