I need to use the following data to construct an array of hashes. The first element in the array should be:
{
salutation: 'Mr.'
first_name: 'John',
last_name: 'Dillinger',
address: '33 Foolish Lane, Boston MA 02210'
}
The given data is below. I am really struggling to figure out how to do this. Some help would be greatly appreciated as I am currently at wits end!!!
salutations = [
'Mr.',
'Mrs.',
'Mr.',
'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'
]
The only solution i was able to come up with doesn't work. Any idea why???
array_of_hashes = []
array_index = 0
def array_to_hash (salutations, first_names, last_names, addresses)
while array_index <= 5
hash = {}
hash[salutation] = salutations(array_index)
hash[first_name] = first_names(array_index)
hash[last_name] = last_names(array_index)
hash[address] = addresses(array_index)
array_of_hashes << hash
array_index += 1
end
end
array_to_hash(salutations,first_names,last_names,addresses)
EDIT - With help from you guys I was able to get my solution to work:
def array_to_hash (salutations, first_names, last_names, addresses)
array_of_hashes = []
array_index = 0
while array_index <= 4
hash = {}
hash[:salutation] = salutations[array_index]
hash[:first_name] = first_names[array_index]
hash[:last_name] = last_names[array_index]
hash[:address] = addresses[array_index]
array_of_hashes << hash
array_index += 1
end
puts array_of_hashes
end
array_to_hash(salutations,first_names,last_names,addresses)