I spent some time this evening messing around with Ruby again – I wish I was dedicated enough to work on learning Ruby more frequently because it’s always such a pain to leave it for several weeks/months and then come back to it having forgotten almost everything. Like learning any language, repetition is the key to memorizing the syntax.
Anyway, here’s some code I slapped together this evening – something to track my run dates and distances. The added value in this code, versus my previous work with hashes, is that this offers the user a choice of options up front (add, update, display, delete).
run_miles = { "033016" => 2, "032916" => 1.85, "032816" => 1, "032116" => 3.5,} puts "What would you like to do?" puts "-- Type 'add' to add a run." puts "-- Type 'update' to update a run." puts "-- Type 'display' to display all runs." puts "-- Type 'delete' to delete a run." choice = gets.chomp case choice when "add" puts "What is today's date in mmddyy format?" date = gets.chomp if run_miles[date].nil? puts "How many miles did you run?" miles = gets.chomp run_miles[date] = miles puts "A run on #{date} for #{miles} has been entered!" else puts "A run on #{date} of #{miles} has already been recorded!" end when "update" puts "Enter the date in mmddyy format." date = gets.chomp if run_miles[date].nil? puts "Date not found!" else puts "What is the new mileage?" miles = gets.chomp run_miles[date] = miles puts "The run on #{date} has been updated to #{miles} miles!" end when "display" run_miles.each do |date, miles| puts "#{date}: #{miles}" end when "delete" puts "Which date do you want to delete?" date = gets.chomp if run_miles[date].nil? puts "Date not found!" else run_miles.delete(date) puts "#{Date} has been removed." end else puts "Sorry, I didn't understand you." end
It’s a cute little program – downloaded Ruby 2.2.4 on my Windows machine and ran it from the command line. I’d like to start getting into real web-based stuff soon.
Leave a comment