ruby - Why does it output one document, when it used to output multiple? -
This list outputs documents for each person in the list. But since I had added the code to determine the most popular date & amp; Time for the list of given dates, now it produces only one document for the first person in the list.
def save_thank_you_letters (id, form_letter) Dir.mkdir ("Output") until Dir. is present? ("Output") file name = "output / thank you _ # {ID} .html" file. Open (Filename, 'W'). File | File.puts puts the end of the form_letter "EventManager launches." Content = CSV.open 'event_attendees.csv', header: true, header_converter: symbol template_locate = file. Read "form_letter.erb" erb_template = ERB.new template_layer content.Each | Line | ID = line [0] name = line [: first_name] zipcode = clean_zipcode (line [: zipcode]) phone = clean_phoneAmber (line [: homephone]) legislators = zipod by legislators (zipcode) form_letter = erb_template.result (binding) save_thank_you_letters ( Id, form_letter) # This work is fine until I add this part ... times = contents.map {| Row | Line [: regdate]} target_times = hash [times.group_by do | T | DateTime.Streetum (T, '% M /% D /% Y% H:% M'). Hour end IPAP | K, V [K, v.count] end.sort_by do | K, v | V end.reverse] target_days = Hush [times.group_by do | T | DateTime.Struth (T, '% m /% d /% y% H:% M'). Wday end.map do | K, v | [Date: ABBR_DAYNAMES [k], v.count] end.sort_by do | K, v | V end.reverse] Ending target_times target_day
I think that there is something similar with the way I am processing data from data / time data. If I remove it, then I get an HTML document for each person in the list. But if I'm involved in it, then I'll get the date & amp; The time information I'm looking for - but it only creates a document for the first person in the list.
Can someone please explain what I am doing does not work? I want to print it time and weekdays, but ALSO generates an HTML document for each person in the list.
Thank you!
When you read the CSV file, you read the inner pointer from the line while running. When you reach the end of the file, this indicator resides there, so every time you try to get a new line, you will get zero, as long as you will not take the file again. Therefore, your code began to run on this line:
content.each do | Row |
It received the first line and the cursor was moved to the next line even though you did content.map {...}
inside the loop, Csv reads the file and leaves a curse at the end of the file. So to fix this, you need to move data bits outside of the loop (before or after) and rewind the file before the second iteration:
content.each do | Row | ID = line [0] name = line [: first_name] zipcode = clean_zipcode (line [: zipcode]) phone = clean_phoneAmber (line [: homephone]) legislators = zipod by legislators (zipcode) form_letter = erb_template.result (binding) save_thank_you_letters ( Id, form_letter) end content.wind time = content.map {| Line | Line [: regdate]} target_times = hash [times.group_by do | T | DateTime.Streetum (T, '% M /% D /% Y% H:% M'). Hour end IPAP | K, V [K, v.count] end.sort_by do | K, v | V end.reverse] target_days = Hush [times.group_by do | T | DateTime.Struth (T, '% m /% d /% y% H:% M'). Wday end.map do | K, v | [Date: ABBR_DAYNAMES [k], v.count] end.sort_by do | K, v | V end.reverse] puts target_time as target_ade
Comments
Post a Comment