Menu
Coddy logo textTech

Inventory Snapshot

Part of the Logic & Flow section of Coddy's Ruby journey — lesson 55 of 56.

An inventory report that exercises hashes, Enumerable chains, group_by, and a touch of formatting.

challenge icon

Challenge

Medium

The array inventory is given. Each item is a hash with :name, :category, :qty, and :price.

Print four lines:

  1. Total inventory value across all items (qty * price, summed), printed as an integer.
  2. The category with the highest item count, in the format Top category: <name> (<count>). Ties broken alphabetically by category name.
  3. Names of items out of stock (qty == 0), sorted alphabetically, joined with , . If none, print (none).
  4. Names of the top 3 most expensive items by unit price, highest first, joined with , . Ties broken by name alphabetically.

For the default inventory the output is:

232
Top category: food (3)
milk
chair, lamp, milk

Try it yourself

inventory = [
  { name: "apple",  category: "food",        qty: 10, price: 2 },
  { name: "bread",  category: "food",        qty: 4,  price: 3 },
  { name: "milk",   category: "food",        qty: 0,  price: 4 },
  { name: "chair",  category: "furniture",   qty: 2,  price: 35 },
  { name: "lamp",   category: "furniture",   qty: 5,  price: 20 },
  { name: "pencil", category: "stationery",  qty: 30, price: 1 }
]

# TODO: print total value, top category, out-of-stock names, top 3 by price
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow