Data Transformer
Part of the Logic & Flow section of Coddy's Ruby journey — lesson 56 of 56.
The capstone challenge, reshape a dataset into a summary report. Combine filtering, mapping, grouping, error handling, and method definition into one program.
Challenge
MediumThe array students is given. Each entry is a hash with :id, :grades (an array of integers, possibly empty), and :subjects (an array of strings).
Define a method transform_dataset(students) that returns a hash with two keys:
:qualified_ids: sorted array of:ids of students who have at least one grade and all grades strictly above70(useall?).:subject_counts: a hash mapping each subject taken by the qualified students to the number of qualified students taking it.
If a student's :grades array is empty, they are not qualified, silently skip them.
Then call the method and print:
- The qualified IDs joined with
,, prefixed withQualified:. If none, printQualified: (none). - One line per subject (sorted alphabetically) in the format
<subject>: <count>.
For the default students, the output is:
Qualified: S1, S4
biology: 1
history: 1
math: 2
physics: 1Cheat sheet
Combine filtering, mapping, grouping, and output formatting in Ruby:
def transform_dataset(students)
qualified = students.select do |s|
s[:grades].any? && s[:grades].all? { |g| g > 70 }
end
qualified_ids = qualified.map { |s| s[:id] }.sort
subject_counts = Hash.new(0)
qualified.each do |s|
s[:subjects].each { |subj| subject_counts[subj] += 1 }
end
{ qualified_ids: qualified_ids, subject_counts: subject_counts }
end
Print results with fallback and sorted keys:
result = transform_dataset(students)
ids = result[:qualified_ids]
puts "Qualified: #{ids.any? ? ids.join(', ') : '(none)'}"
result[:subject_counts].sort.each do |subject, count|
puts "#{subject}: #{count}"
end
all?returnsfalseon empty arrays — useany?first to guard against empty grades.Hash.new(0)initializes missing keys to0, useful for counting..sorton a hash returns sorted[key, value]pairs alphabetically.
Try it yourself
students = [
{ id: "S1", grades: [85, 92, 78], subjects: ["math", "physics"] },
{ id: "S2", grades: [65, 80, 90], subjects: ["math", "biology"] },
{ id: "S3", grades: [], subjects: ["art"] },
{ id: "S4", grades: [88, 95], subjects: ["math", "biology", "history"] },
{ id: "S5", grades: [70, 75], subjects: ["chemistry"] }
]
# TODO: def transform_dataset(students), return { qualified_ids:, subject_counts: }
# TODO: print qualified IDs and subject counts
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String Methods OverviewString InterpolationIterating Over StringsSplit and JoinRecap - String Weaver4Blocks, Procs & Lambdas
What is a Block?do..end vs BracesThe yield KeywordBlock ParametersProcs and LambdasRecap - Custom Iterator7Hashes Part 2
Hash.new with DefaultsIterating HashesNested HashesMerging and TransformingRecap - Frequency Counter10Project - Student Records
Project OverviewAdd Student5Enumerable Powerhouse
Select and RejectChaining MapReduce / Injectcount, all?, any?, none?group_by and partitionsort_by, min_by, max_byRecap - Data Pipeline8Advanced Decision Making
Case with Classes & RegexMulti-value whenTernary OperatorInline if / unlessRecap - Grade Classifier