From 3f8f3b6acf961e06d8c5493475c7ddbe961fa2bc Mon Sep 17 00:00:00 2001 From: usure Date: Thu, 7 Apr 2016 11:36:53 -0400 Subject: [PATCH] Update text_editor.rb Made formatting nicer (imo) You can also now pick the name of the file you want to write. Instead of the generic "text_editor_save.txt." Removed "@save_file" and replaced it with @file_name. --- text_editor.rb | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/text_editor.rb b/text_editor.rb index 9219689..392e1c4 100644 --- a/text_editor.rb +++ b/text_editor.rb @@ -1,6 +1,6 @@ class TextEditor def initialize - @save_file = "text_editor_save.txt" +# @save_file = "text_editor_save.txt" @text = [] run end @@ -11,10 +11,8 @@ def run greet prompt_load - puts puts 'Type "quit" to stop entering text.' puts - loop do line = gets.chomp break if line == "quit" @@ -28,12 +26,12 @@ def run def greet greeting = %q( - _____ _ _____ _____ _______ _______ + _____ _ _____ _____ _______ _______ / __ \| | |_ _| |_ _| ___\ \ / /_ _| - | / \/| | | | | | | |__ \ V / | | - | | | | | | | | | __| / \ | | - | \__/\| |_____| |_ | | | |___/ /^\ \ | | - \____/\_____/\___/ \_/ \____/\/ \/ \_/ + | / \/| | | | | | | |__ \ V / | | + | | | | | | | | | __| / \ | | + | \__/\| |_____| |_ | | | |___/ /^\ \ | | + \____/\_____/\___/ \_/ \____/\/ \/ \_/ ) puts greeting @@ -44,39 +42,46 @@ def prompt_load input = nil until input == "N" || input == "L" puts "Would you like to start a (N)ew file or (L)oad an existing file?" - puts input = gets.chomp.upcase end load if input == "L" + write if input == "N" end def load - if File.exist?(@save_file) - file = File.open(@save_file, "r") + puts "File name?" + @file_name = gets.chomp + if File.exist?(@file_name) + file = File.open(@file_name, "r") file.each_line {|line| @text << line} + print else - puts "No saved file exists. Starting new file." + puts "No saved file exists. Would you like to create it?" + write end puts end - - def print + def write + puts "File name?" + @file_name = gets.chomp + file = File.open(@file_name, "w") puts - puts "You wrote:" + load + end + def print + puts "file: #{@file_name}:" @text.each_with_index {|sentence, line| puts "#{line+1} #{sentence}"} puts end def prompt_edit puts "Would you like to edit a line? (Y/N)" - puts response = gets.chomp.upcase if response == "Y" line = nil until (1..@text.length).include? line - puts puts "Which line would you like to edit?" line = gets.chomp.to_i end @@ -86,7 +91,6 @@ def prompt_edit end def edit(line) - puts puts "Was:" puts @text[line-1] puts @@ -107,9 +111,9 @@ def prompt_save end def save - file = File.open(@save_file, "w") + file = File.open(@file_name, "w") file.puts(@text) end end -TextEditor.new \ No newline at end of file +TextEditor.new