Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ruby Enumerables - Hashes add the code where it says your code here # RubyHashes # Part I def array_2_hash emails, contacts # YOUR CODE

Ruby Enumerables - Hashes

add the code where it says your code here

# RubyHashes # Part I def array_2_hash emails, contacts # YOUR CODE HERE end

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

# Part III def hash_2_array contacts # YOUR CODE HERE end

-------------------------------------------------------------

do not write in this code

require 'hashes.rb'

RSpec.configure do |config| config.filter_run_excluding :disabled => true end

describe 'Ruby Hashes Part I' do

describe "array_2_hash" do it "should be defined" do expect { array_2_hash(["bobsmith@example.com","sallyfield@example.com","markdole@example.com"], {'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''}) }.not_to raise_error end it "returns the correct hash [20 points]" , points: 20 do expect(array_2_hash(["bobsmith@example.com","sallyfield@example.com","markdole@example.com"], {'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''})).to be_a_kind_of Hash expect(array_2_hash(["bobsmith@example.com","sallyfield@example.com","markdole@example.com"], {'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''})).to eq({'Bob Smith':'bobsmith@example.com', 'Sally Field':'sallyfield@example.com', 'Mark Dole':'markdole@example.com'}) end it "works on the empty array [10 points]" , points: 10 do expect { array_2_hash([], {'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''}) }.not_to raise_error expect(array_2_hash([], {'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''})).to eq({'Bob Smith':'', 'Sally Field':'', 'Mark Dole':''}) end end end

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

describe 'Ruby Hashes Part III' do

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

-----------------------------------------------------------------------------

quiz questions

1.)How many methods are there in the lib/hashes.rb file?

2.)How many describe blocks are in the RSpec definition file?

3.)How many test cases are defined in the array_2_hash collection?

How many test cases are defined in the array2d_2_hash collection?

How many test cases are defined in the hash_2_array collection?

4.)Select ALL that apply. Which collections will be excluded when we run the rspec tests?

array_2_hash
array2d_2_hash

hash_2_array

------------------------------------------------------------

5.)

Look at the array_2_hash method in the lib/hashes.rb file.

You will notice that this method takes two inputs.

emails: an array of email addresses of type string. Example: ["bobsmith@example.com"]

contacts: a hash for contacts. The keys for this hash will be the contact names as type string and the values will be the contact email addresses as type strings. Example: {"Bob Smith":"bobsmith@example.com"}

Assumptions: The array and 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 emails populated from the array.

Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

6.)

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.

7.)

Remove the , :disabled => true from the hash_2_array collection in the spec/hashes_spec.rb file. Don't forget to save your changes!

Now look at the hash_2_array method in the lib/hashes.rb file.

You will notice that this method takes one input.

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"}

Hint: Look at the rspec tests to see the input that will be tested and the expected output.

Write the Ruby code in this method that will create three arrays of type string from the hash and then will return an array that contains these three arrays (a two dimensional array!).

Assumption: Your returned array will contain the inner arrays in the following order: emails, phones, names.

Example:

[["bobsmith@example.com"],["555-555-5555"],["Bob Smith"]]

Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

1. Always guess when only right answers are scored.

Answered: 1 week ago

Question

4. What are the current trends in computer software platforms?

Answered: 1 week ago