Evelyn Seyer Evelyn Seyer - 1 year ago 37
Ruby Question

Find the name and age of the oldest person in a txt file using ruby

"Attached is a file with people's names and ages.
There will always be a First name and Last name followed by a colon then the age.
So each line with look something like this.
FirstName LastName: Age

Your job is write a ruby program that can read this file and figure out who the oldest person/people are on this list. Your program should print out their name(s) and age(s)."

This is the code I have so far:

File.open('nameage.txt') do |f|
f.each_line do |line|
puts line.split(":").last.to_i

With this, I am able to separate the name from the age but I don't know how to get the highest value and print out the highest value with name and age.
Please help!


"figure out who the oldest person/people are on this list", so multiple results are possible. Ruby has a group_by method, which groups an enumerable by a common property. What property? The property you specify in the block.

grouped = File.open('nameage.txt') do |f|
  f.group_by do |line|
    line.split(":").last.to_i  # using OP's line

p grouped                      # just to see what it looks like    
puts grouped.max.last          # end result