Grocery Unit Price Comparator CLI
Build a Ruby command-line tool that helps you find the best deal at the supermarket: enter products with their price and package size (such as 500 g for 3.20, 1 kg for 5.90, or 16 oz for 2.99) and it converts everything to the same unit, calculates the price per kilogram, liter or piece, and ranks them from cheapest to most expensive. It handles metric and imperial units and refuses to compare things that can't be compared, like grams with liters. It's a great first Ruby project: you'll learn classes and methods, hashes for unit conversion tables, reading and checking user input, sorting, formatting a table, and testing with Minitest.
- Estimate
- ~5.5h
- Steps
- 5
- Completed by
- 0
- Proposed by
- codeseed.app
Standard Library
Project roadmap
- 01
Model a grocery item
~1hCreate a GroceryItem class with a name, price, quantity and unit, validating that the price and quantity are positive numbers and the unit is known. Add a method that returns the price per base unit (per kilogram, liter or piece), using BigDecimal or Rational so prices are exact. Done when: an item's price per unit is correct for a few examples calculated by hand.
- 02
Convert units
~1.5hCreate a conversion table (a hash) mapping each unit to its kind (weight, volume or count) and its value in the base unit: g, kg, oz and lb for weight; ml, l, fl oz and gal for volume; and pcs for pieces. Accept common spellings (gram, grams and g). Raise a clear error for unknown units. Done when: 16 oz is converted to 0.4536 kg, and unknown units are rejected.
- 03
Enter items
~1hWrite an input loop that asks for items one per line in a simple format such as Milk, 1.29, 1 l (or name, price, quantity and unit asked separately), re-asking with a clear message when a line is invalid, until the user enters an empty line. Also accept a CSV file of items as a command-line argument. Done when: several items can be entered or loaded from a file.
- 04
Print the ranked comparison
~1hGroup items by kind (only items of the same kind are compared), sort each group by price per base unit, and print an aligned table with the name, package, price and price per unit, marking the best deal and showing how much more the others cost in percent. Done when: the table ranks items correctly and highlights the best deal.
- 05
Test it
~1hWrite Minitest tests: price per unit for every unit, conversions between metric and imperial, unknown units, invalid input lines, grouping by kind, and the ranking order. Done when: the tests pass.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack