我正在写一个小备忘录脚本,在运行并测试之后,它没有输出到文件,它让我拉出我的头发,因为我没有改变修复问题。文件已创建,但保持为空。
require 'ruby-progressbar'
puts "Loading..."
lt = ProgressBar.create
if File.directory?("./memos") == false
lt.progress = 50
Dir.mkdir("memos")
lt.progress = 100
end
lt.progress = 100
Dir.chdir("memos")
puts "Welcome to Eternal Memorizer."
loop do
progress = ProgressBar.create
progress.log "Getting files..."
progress.progress = 50
cdf = Dir.glob("**/*")
progress.progress = 100
puts "Memos:"
puts cdf
puts ""
puts "[M]ake memo, [R]ead memo, [D]elete memo."
op = gets.chomp
if op.downcase == "m"
print "Memo name: "
nam = gets.chomp
puts "\nText, Press enter to finish:"
txt = gets.chomp
num = txt.length
txt = txt.split('')
ggg = ProgressBar.create(:title => "Saving...", :total => num)
o = File.open("#{nam}.mem", 'w+')
i = 0
ggg.log "Writing #{txt.join}"
num.times do
puts txt
#sleep(30)
o.write("#{txt[i]}")
i += 1
ggg.increment
ggg.title = "#{txt[i]}"
#system("cls"); system("clear")
end
elsif op.downcase == "r"
puts "Which memo to open?"
fil = gets.chomp
begin
fil = File.open("#{fil}", 'r+')
puts "\n-=-=-=-"
puts fil.read
puts "-=-=-=-\n"
rescue
puts "Err, unable to open file."
end
elsif op.downcase == "d"
puts "Which memo to delete?"
File.delete(gets.chomp)
end
end
我已经阅读并重读了它,而且它已经达到我放弃它的程度,所以如果发现错误,这将是一个很大的帮助。
答案 0 :(得分:1)
在写完文件后手动关闭文件。
o.close
这应该将它提交给磁盘。
或者使用File.open
的块形式来处理文件的关闭。
File.open("#{nam}.mem", 'w+') do |o|
i = 0
ggg.log "Writing #{txt.join}"
num.times do
puts txt
#sleep(30)
o.write("#{txt[i]}")
i += 1
ggg.increment
ggg.title = "#{txt[i]}"
#system("cls"); system("clear")
end
end