Object-oriented programming is fundamental to building maintainable Ruby applications. This drill teaches you to design classes with initialization, instance methods, state management, and data validation. You'll create an inventory system that tracks products with names, quantities, and prices, learning class design, attr_accessor usage, instance variables, and methods that modify object state—essential skills for building real-world Ruby applications.
attr_reader :name creates a getter method for @name
attr_accessor :quantity creates both getter and setter for @quantity
Use @variable for instance variables inside classes
array.find { |item| condition } returns first matching item
Use .downcase for case-insensitive string comparison
array.sum(&:method_name) sums results of calling method on each item
Format price with '%.2f' % number for 2 decimal places
File.readlines(file).each reads and iterates over each line
process_inventory('inventory.txt')
INVENTORY REPORT ================================================== Laptop: Qty 15 @ $999.99 each (Total: $14999.85) Mouse: Qty 40 @ $29.99 each (Total: $1199.60) -------------------------------------------------- Total Inventory Value: $16199.45
process_inventory('inventory.txt')
INVENTORY REPORT ================================================== Laptop: Qty 15 @ $999.99 each (Total: $14999.85) Mouse: Qty 50 @ $29.99 each (Total: $1499.50) Keyboard: Qty 25 @ $79.99 each (Total: $1999.75) -------------------------------------------------- Total Inventory Value: $18499.10
process_inventory('inventory.txt')
INVENTORY REPORT ================================================== Widget: Qty 0 @ $5.00 each (Total: $0.00) -------------------------------------------------- Total Inventory Value: $0.00
Console output will appear here...
Are you sure?
You're making great progress