David, the general idea of what you have to do to achieve your goals is to first process the data:
- Read data from data.csv file
- Throw out the first two lines
- Convert each String to an Array with 3 Strings
- Filter the data you want: Leave the name of the people, convert the String with the date to a Date type Object, send the numbers away.
Once the data is filtered, we just need to sort/group it the way we want it.
The snippet that solves your problem is this:
require 'date'
# Ler e formatar dados
formated_data = File.readlines('data.csv')[2..-1]
.map { |line| line.split() }
.map { |fields| [fields[0], Date.parse(fields[1])] }
# Agrupar e ordenar dados
# Maior data:
p formated_data.max_by{ |fields| fields[1] }
# Menor data:
p formated_data.min_by{ |fields| fields[1] }
# Pessoas nasceram na mesma data
p formated_data.group_by { |fields| fields[1] }
Now let’s see what makes each line:
Now that you have the data turned into objects, it’s easy to transform it so that they group/filter in the way that they answer your questions:
To get the maximum date, just say that you want the maximum of the array with the method #max_by
and applies it to the second array field ( the one with the date) . Docs: max_by
The same thing to get the bare minimum, but use the method instead #min_by
. Docs: min_by
Finally to group by date, use the #group_by method. Docs: #group_by