Solution: Mail Merge Birthday Invitation Cards
Explore how to implement a mail merge system in Ruby by creating personalized birthday invitation cards. Learn to manipulate template strings, iterate over guest lists, and generate individual files for each invitee using practical file handling methods.
We'll cover the following...
We'll cover the following...
Solution
guest_list = ["Pokkle", "Angela", "Tonpa", "Toby", "Biscuit", "Mito", "Kate", "Renee", "Chloe", "Kelly", "Melody"]
invitation = "Dear {{first_name}},
I am celebrating my 12th Birthday on the 1st of April!
Come celebrate with me!
Where: 42 Greed-Island Street, Yorkshin City
When: 2PM to 5PM
RSVP: 24th of March (0400-000-000 or rsvpjessica@example.com)
Hope to see you there,
Jessica."
guest_list.each do |name|
named_invitation = invitation.gsub("{{first_name}}", name)
File.open("./files/#{name.downcase}_invitation.txt", "w").write(named_invitation)
end
puts "\n\nUse ls and cat commands to list and examine the created files."Generating text files for the birthday invitation cards
Explanation
Line 1: An array containing the names of guests is created.
Lines 3–14: ...