Skip to content

Instantly share code, notes, and snippets.

@jli-hashrocket
Created November 19, 2013 16:55
Show Gist options
  • Select an option

  • Save jli-hashrocket/7548574 to your computer and use it in GitHub Desktop.

Select an option

Save jli-hashrocket/7548574 to your computer and use it in GitHub Desktop.
mailing list with optional challenge completed
=begin
salutations = [
'Mr.',
'Mrs.',
'Mr.',
'Dr.',
'Ms.'
]
=end
salutations = [
['Mr.', 'Mrs.'],
'Mrs.',
['Mr.', 'Mrs.'],
['Dr.'],
'Ms.'
]
first_names = [
'John',
'Jane',
'Sam',
'Louise',
'Kyle'
]
last_names = [
'Dillinger',
'Cook',
'Livingston',
'Levinger',
'Merlotte'
]
addresses = [
'33 Foolish Lane, Boston MA 02210',
'45 Cottage Way, Dartmouth, MA 02342',
"54 Sally's Court, Bridgewater, MA 02324",
'4534 Broadway, Boston, MA 02110',
'4231 Cynthia Drive, Raynham, MA 02767'
]
contacts = salutations.zip(first_names,last_names,addresses)
contact_hash = {}
contacts.each do |contact|
contact_hash[:contact] = contact
title = contact_hash[:contact][0].is_a?(Array) ? contact_hash[:contact][0].join(" and ") : contact_hash[:contact][0]
puts title + " " + contact_hash[:contact][1] + " " + contact_hash[:contact][2]
puts contact_hash[:contact][3]
end
@HeroicEric
Copy link
Copy Markdown

Slightly longer, but I think refactoring the last few lines to read this way is more readable:

contacts =  salutations.zip(first_names,last_names,addresses)

contacts.each do |contact|
  salutation = contact[0]
  salutation = salutation.is_a?(Array) ? salutation.join(" and ") : salutation

  first_name = contact[1]
  last_name = contact[2]
  address = contact[3]

  puts "#{salutation} #{first_name} #{last_name}"
  puts address
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment