简体   繁体   English

将Ruby输出写入文件

[英]Write Ruby Output to a File

I have a script that combs through a particular folder and finds all of the documents that have been modified today: 我有一个脚本,梳理一个特定的文件夹,并找到今天已修改的所有文件:

Dir.glob("/path/to/folder/*/*.txt") do |file|
    f = File.open(file.strip)
    lines = f.readlines
    mod = f.mtime
    modtime = f.mtime.strftime("%I:%M%p")
    text = lines.join
    wordcount = text.split.length
    project = File.basename(file).gsub(/.txt/, ' ').strip
    if mod > (Time.now - 86400)
        found_completed = true
        entry = "#{modtime} - #{project} - #{wordcount}"
    end
    if found_completed == false
    puts "not today"
    end
    if found_completed == true
    puts "worked on #{entry}"
    end
end

That all works fine. 一切正常。 However, I also went to write that multi-line output to a file. 但是,我还将多行输出写入文件。 When I add this to the end of the script (before the final 'end') it comes up blank: 当我将它添加到脚本的末尾(在最后的'结束'之前)时,它会显示为空白:

open('/path/to/newfile.txt', 'w') { |f|
  f.puts ("#{entry}" + "/n/n") }

Any help would be appreciated. 任何帮助,将不胜感激。

You are opening the file every time through the glob loop, overwriting the file, and the last file processed yields a blank entry? 您每次通过glob循环打开文件,覆盖文件,最后处理的文件产生一个空白条目?

You probably want to surround the glob with the file opening, so it only gets opened once, putting the f.puts line where it is now. 您可能希望用文件打开来包围glob,因此它只会打开一次,将f.puts行放在现在的位置。

edit 编辑

This is a little more idiomatic ruby... break it up into a class, keep the parts small and isolated in meaning, give it intention revealing function names. 这是一个更为惯用的红宝石......将其分解为一个类,保持部件小而孤立的意义,给它意图揭示功能名称。 I'm sure there's more to be done, but I think this makes it easier to read. 我确信还有更多工作要做,但我认为这样可以更容易阅读。

class FileInfo
  def initialize(file)
    @file = File.open(file)
  end

  def wordcount
    @file.read.scan(/\w/).size
  end

  def mtime
    @file.mtime
  end

  def formatted_mtime
    @file.mtime.strftime("%I:%M%p")
  end

  def project
    File.basename(@file.path).gsub(/.txt/, ' ').strip
  end

  def recent?
    @file.mtime > (Time.now - 86400)
  end
end

open('logfile.txt', 'w')  do |log|
  Dir.glob("/path/to/folder/*/*.txt") do |file|
    fi = FileInfo.new(file)
    if fi.recent?
      log.puts "worked on #{fi.formatted_mtime} - #{fi.project} - #{fi.wordcount}"
    else
      log.puts "not today"
    end
  end
end

Just change variable name f to ff , and do: 只需将变量名称f更改为ff ,然后执行:

entry = nil
if mod > (Time.now - 86400)
   found_completed = true
   entry = "#{modtime} - #{project} - #{wordcount}"
end
open('/path/to/newfile.txt', 'a+') {|ff| ff.puts entry }

or: 要么:

if mod > (Time.now - 86400)
   found_completed = true
   entry = "#{modtime} - #{project} - #{wordcount}"
   open('/path/to/newfile.txt', 'a+') {|ff| ff.puts entry }
end

To open file for write/read operations, and then to use it, do: 要打开文件进行写入/读取操作,然后使用它,请执行以下操作:

fstore = open '/path/to/newfile.txt', 'a+'
...
fstore.puts entry
...
fstore.close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM