Question: In Ruby write a function that takes a string which contains data in CSV format and it will return an array of strings in CSV
In Ruby write a function that takes a string which contains data in CSV format and it will return an array of strings in CSV format. The modifications to the fields are complete but Im struggling to have the elements separated by a space.
Expected return value: GenderFirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order AtMaleCarl,Wilderman,carl,yahoo.com,Seattle,Safari iPhone,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,Detroit,Chrome Android,afternoon", "Female,Shanelle,Marquardt,shanelle,hotmail.com,Las Vegas,Chrome,afternoon", "Female,Lavonne,Romaguera,lavonne,yahoo.com,Seattle,Chrome,morning", "Male,Derick,McLaughlin,derick,hotmail.com,Chicago,Chrome Android,afternoon"
require 'date'
#
def agerangeage
myage
case age.toi
when
myage
when
myage
when
myage
when
myage
else
myage "NOAGE"
end
return myage
end
# morning :am :amafternoon :pm :pmevening :pm :pm
def timeofdaydatetime
t DateTime.parsedatetimeYmd H:M:S
th tstrftimeHtoi
if th && th
return "morning"
elsif th && th
return "afternoon"
elsif th && th
return 'evening'
else
return "NONE"
end
end
def mydatatransformcsvcontent
arr csvcontent.split
index ;
values
while index arr.length
valuesindex arrindexsplit;
index
end
values.map&:join
# puts values
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wildermancarl@yahoo.com,Seattle,Safari iPhone,::
# Male,Marvin,Lind,marvin,marvinlind@hotmail.com,Detroit,Chrome Android,::
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,Las Vegas,Chrome,::
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,Seattle,Chrome,::
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,Chicago,Chrome Android,::
# puts arr
# puts valuesjoin
index
while index values.length
valuesindex valuesindexsplit@ # remove username from email
valuesindex agerangevaluesindex # age range
valuesindex timeofdayvaluesindex # morning, afternoon, or evening
index
end
nvals
index
while index values.length
nvalsindex valuesindexjoin
index
end
# puts nvals
return nvals
end
mydatatransformGenderFirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
Male,Carl,Wilderman,carl,wildermancarl@yahoo.com,Seattle,Safari iPhone,::
Male,Marvin,Lind,marvin,marvinlind@hotmail.com,Detroit,Chrome Android,::
Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,Las Vegas,Chrome,::
Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,Seattle,Chrome,::
Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,Chicago,Chrome Android,::
# input:
# Gender,FirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At
# Male,Carl,Wilderman,carl,wildermancarl@yahoo.com,Seattle,Safari iPhone,::
# Male,Marvin,Lind,marvin,marvinlind@hotmail.com,Detroit,Chrome Android,::
# Female,Shanelle,Marquardt,shanelle,marquardt.shanelle@hotmail.com,Las Vegas,Chrome,::
# Female,Lavonne,Romaguera,lavonne,romaguera.lavonne@yahoo.com,Seattle,Chrome,::
# Male,Derick,McLaughlin,derick,mclaughlin.derick@hotmail.com,Chicago,Chrome Android,::
# expected output return with spaces between rows:
# GenderFirstName,LastName,UserName,Email,Age,City,Device,Coffee Quantity,Order At "Male,Carl,Wilderman,carl,yahoo.com,Seattle,Safari iPhone,afternoon", "Male,Marvin,Lind,marvin,hotmail.com,
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
