Recap - Data Pipeline
Part of the Logic & Flow section of Coddy's Ruby journey — lesson 27 of 56.
One challenge that runs a small dataset through a chain of Enumerable methods, the way you'll write Ruby for real.
Challenge
MediumThe array orders is given, each element is a hash with :product, :qty, and :price. Print three lines:
- The total revenue across all orders (
qty * price, summed). Usemap+reduce(:+), orsum { ... }. - The number of orders with
qty >= 3(usecount). - The product name of the order with the highest revenue (use
max_by).
For the default array, the output is:
59
2
milkTry it yourself
orders = [
{ product: "apple", qty: 4, price: 2 },
{ product: "bread", qty: 1, price: 3 },
{ product: "milk", qty: 6, price: 4 },
{ product: "cheese", qty: 2, price: 12 }
]
# TODO: total revenue, count of orders with qty >= 3, top-revenue product
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