Question

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

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top