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
process_inventory('inventory.txt')
INVENTORY REPORT ================================================== Laptop: Qty 12 @ $999.99 each (Total: $11999.88) Mouse: Qty 55 @ $29.99 each (Total: $1649.45) Keyboard: Qty 30 @ $79.99 each (Total: $2399.70) -------------------------------------------------- Total Inventory Value: $16049.03
p = Product.new('Test', 10, 5.00)
p.add_stock(5)
puts p.to_s
Test: Qty 15 @ $5.00 each (Total: $75.00)
p = Product.new('Test', 5, 10.00)
puts p.remove_stock(10)
puts p.quantity
false 5
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept