Frage

I'm very surprised I couldn't find an answer to this question, but I need to select all files modified between Time.now and 1.day.ago. Obviously I'm not expecting an 'ago' operator when dealing with the ruby File or FileUtil classes, but if somebody showed me one I wouldn't be surprised. :)

The other way would be an array function I think...perhaps involving the '<=>' operator which I have never used

War es hilfreich?

Lösung

Perhaps something like this? (untested):

selected_files = Dir.glob("*.pdf").select do |file|
  mtime = File.mtime(file)

  # if in a rails environment:
  # (1.day.ago .. Time.now).cover?(mtime)

  # if not in rails environment but want to use that code do this before that line:
  # require 'active_support/all'

  # else do the math:
  # mtime > (Time.now - 86400) and mtime < Time.now
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top