I've prepared a method which parses a log file and returns a hash as a result:
def parse
@hash = Hash.new { |hash, key| hash[key] = [] }
File.open(file_path).each do |line|
customer, product = line.split
@hash[customer] << product
end
@hash
end
result:
=> {"customer_id$1"=>["product_id$184", "product_id$184", "product_id$94", "product_id$16",
"product_id$184", "product_id$592"],
"customer_id$3"=>["product_id&16", "product_id&196", "product_id&196", "product_id&82"],
"customer_id$6"=>["product_id$26", "product_id$126", "product_id$26", "product_id$26"]}
How to count all (e.g. customer_id$1 = 6) and unique values (e.g. customer_id$1 = 4) for each customer if they are an array? I'll have this code in AWS lambda so I must use pure Ruby 2.7.
@hash.keys.count.