Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created July 19, 2013 22:54
Show Gist options
  • Save TGOlson/6042940 to your computer and use it in GitHub Desktop.
Save TGOlson/6042940 to your computer and use it in GitHub Desktop.
Simple CRUD program. I've been wanting to learn how to make one of these for a while, so I decided to whip together the most simple version I could - Create, Read, Update and Delete simple text files. Hopefully lots of updates coming.
def create_file(file)
target = File.open(file,'a+')
puts "#{file} created."
end
def read_file(file)
target = File.open(file,'r')
puts "That file is:"
puts target.read
end
def update_file(file)
read_file(file)
target = File.open(file,'a')
puts "What do you want to add?"
print '> '
add = gets.chomp
target.write(add)
puts "Text added."
end
def delete_file(file)
File.delete(file)
puts "File deleted."
end
puts "Do you want to *create*, *read*, *update* or *delete* a file?"
print '> '
command = gets.chomp
puts "What file do you want to #{command}?"
print '> '
file = gets.chomp
create_file(file) if command == 'create'
read_file(file) if command == 'read'
update_file(file) if command == 'update'
delete_file(file) if command == 'delete'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment