Command-line tools are the backbone of automation and system administration. This drill teaches you to build a simple CLI calculator that parses command-line arguments, validates input, performs calculations, and provides helpful usage instructions. You'll learn ARGV parsing, input validation, error handling, and user-friendly CLI design—skills essential for building Ruby scripts, automation tools, and system utilities.
ruby calc.rb <operation> <num1> <num2>ARGV is an array containing command-line arguments
ARGV[0] is the first argument, ARGV[1] is second, etc.
Use Float(string) to convert string to float - raises ArgumentError if invalid
Check array.include?(value) to validate operation
Use case/when for multiple conditions
Check n2 == 0 before dividing
Format with printf style: '%.2f' % number rounds to 2 decimals
rescue ArgumentError catches invalid number conversions
calculate('add', '10', '5')
Result: 15.00
calculate('subtract', '10', '5')
Result: 5.00
calculate('multiply', '10', '5')
Result: 50.00
calculate('divide', '10', '5')
Result: 2.00
calculate('divide', '10', '0')
Error: Cannot divide by zero
Console output will appear here...
Are you sure?
You're making great progress