Question: In Ruby, create a hash of a hash map with the total occurrences of key values to output in a JSON style as shown. require

In Ruby, create a hash of a hash map with the total occurrences of key values to output in a JSON style as shown.
require 'json'
require 'csv'
def my_data_process(str)
arr = str.split("\",\"")
index =0
values =[]
while (index < arr.length)
values[index]= arr[index].split(",");
index +=1
end
# count occurrances of Gender: {Male, Female}, Email, Age, City, Device, Order At
# create map with totals
arr_map ={}
arr_map[:Gender]={ :Male =>0, :Female =>5}
arr_map[:Email]={ :domain1=>0, :domain2=>5}
arr_map[:Age]={ :a =>0, :b =>5, :c =>1}
arr_map[:City]={ :Male =>0, :Female =>5}
arr_map[:Device]={ :Male =>0, :Female =>5}
arr_map[:"Order At"]={ :Male =>0, :Female =>5}
json = JSON.pretty_generate(arr_map)
s = json.gsub /[
]/,''
s.delete! '\\'
s
end
my_data_process('["Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At", "Male,Carl,Wilderman,carl,yahoo.com,21->40,Seattle,Safari iPhone,2,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,66->99,Detroit,Chrome Android,2,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,21->40,Las Vegas,Chrome,1,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,66->99,Seattle,Chrome,2,morning", "Male,Derick,McLaughlin,derick,hotmail.com,41->65,Chicago,Chrome Android,1,afternoon"]')
# Input: ["Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At", "Male,Carl,Wilderman,carl,yahoo.com,21->40,Seattle,Safari iPhone,2,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,66->99,Detroit,Chrome Android,2,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,21->40,Las Vegas,Chrome,1,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,66->99,Seattle,Chrome,2,morning", "Male,Derick,McLaughlin,derick,hotmail.com,41->65,Chicago,Chrome Android,1,afternoon"]
#
# Expected Output (return):
# '{"Gender":{"Male":3,"Female":2},"Email":{"yahoo.com":2,"hotmail.com":3},"Age":{"21->40":2,"66->99":2,"41->65":1},"City":{"Seattle":2,"Detroit":1,"Las Vegas":1,"Chicago":1},"Device":{"Safari iPhone":1,"Chrome Android":2,"Chrome":2},"Order At":{"afternoon":4,"morning":1}}'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!