Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Remove the , :disabled => true from the array2d_2_hash collection in the spec/ha

ID: 2247404 • Letter: R

Question

Remove the , :disabled => true from the array2d_2_hash collection in the spec/hashes_spec.rb file. Don't forget to save your changes! Now look at the array2d_2_hash method in the lib/hashes.rb file. You will notice that this method takes two inputs. contact_info: a two dimensional array of email addresses and phone numbers of type string. Example: [["bobsmith@example.com", "555-555-5555"]] contacts: a hash for contacts. The keys for this hash will be the contact names as type string and the values will be an inner hash that will hold the email address (with "email" as key) as type string and phone number (with "phone" as key) as type string. Example: {:"Bob Smith"=>{:email=>"bobsmith@example.com", :phone=>"555-555-5555"}} Assumptions: The two dimensional array and the hash both contain the same number of items and will be already in the correct order. Hint: Look at the rspec tests to see the inputs that will be tested.

Write the Ruby code in this method that will return the contacts hash with the email address and phone number hash populated for each contact. Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

hashes_specs.rb

describe 'Ruby Hashes Part II' do

describe "array2d_2_hash", :disabled => true do
it "should be defined" do
expect { array2d_2_hash([["bobsmith@example.com", "555-555-5555"],["sallyfield@example.com","111-111-1111"]], {'Bob Smith':{}, 'Sally Field':{}}) }.not_to raise_error
end
  
it "returns the correct hash [30 points]" , points: 30 do
expect(array2d_2_hash([["bobsmith@example.com", "555-555-5555"],["sallyfield@example.com","111-111-1111"]], {'Bob Smith':{}, 'Sally Field':{}})).to be_a_kind_of Hash
expect(array2d_2_hash([["bobsmith@example.com", "555-555-5555"],["sallyfield@example.com","111-111-1111"]], {'Bob Smith':{}, 'Sally Field':{}})).to eq({:"Bob Smith"=>{:email=>"bobsmith@example.com", :phone=>"555-555-5555"}, :"Sally Field"=>{:email=>"sallyfield@example.com", :phone=>"111-111-1111"}})
end
  
it "works on the empty array [10 points]" , points: 10 do
expect { array2d_2_hash([[]], {'Bob Smith':{}, 'Sally Field':{}}) }.not_to raise_error
expect(array2d_2_hash([[]], {'Bob Smith':{}, 'Sally Field':{}})).to eq({:"Bob Smith"=>{}, :"Sally Field"=>{}})
end
end
end

hashes.rb

#part II
def array2d_2_hash contact_info, contacts
# YOUR CODE HERE
end

Explanation / Answer

Completed code for hashes.rb:

def array2d_2_hash contact_info, contacts

i=0
contact_info.each do |k, v|
data=Hash.new
data[:email]=contacts[i][0]
data[:phone]=contacts[i][1]
  
contact_info[k] = data
i=i+1
end
puts contact_info

end

Sample output:

{:"Bob Smith"=>{:email=>"bobsmith@example.com", :phone=>"555-555-5555"}, :"Sally Field"=>{:email=>"sallyfield@example.com", :phone=>"111-111-1111"}}