Created
July 19, 2013 22:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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