Skip to content

Instantly share code, notes, and snippets.

@VickiLanger
Created May 23, 2022 15:31
Show Gist options
  • Save VickiLanger/a7df42da9677cef9b6f46d4a3800f55a to your computer and use it in GitHub Desktop.
Save VickiLanger/a7df42da9677cef9b6f46d4a3800f55a to your computer and use it in GitHub Desktop.
Basic crud ops example with book ratings
books = {
"The 57 Bus": 5.0,
"Under The Udala Trees": 4.5,
"Last Night At The Telegaph Club": 5.0,
}
puts "What do you wanna do? type:"
puts "`add` to add a book"
puts "`view` to see all books"
puts "`update` to update a book"
puts "`delete` to delete a book"
command = gets.chomp
case command
when "add"
puts "What book would you like to add?"
title = gets.chomp
puts "From 1-5, how would your rate #{title}?"
rating = gets.chomp
if books[title.to_sym] == nil
books[title.to_sym] = rating.to_i
puts "Added #{title} with a rating of #{rating}!"
else
puts "Oops, you already rated that book"
end
when "view"
books.each {|book, rating| puts "#{book}: #{rating}"}
when "update"
puts "What book rating would you like to update?"
title = gets.chomp
if books[title.to_sym] == nil
puts "you may want to add #{title} as you don't have it yet"
else
puts "How would your rate #{title}?"
rating = gets.chomp
books[title.to_sym] = rating.to_i
puts "Updated #{title} to #{rating}!"
end
when "delete"
puts "What book would you like to delete?"
title = gets.chomp
if books[title.to_sym] == nil
puts "#{title} doesn't exist"
else
books.delete(title.to_sym)
puts "Deleted #{title}"
end
else
puts "Beep boop, error. Are you sure you gave the right command?"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment