Search code examples
ruby-on-railsfilemtime

Find date file was last modified


I am setting up a page that will display the last date the file was modified. From what I understand I need to mtime to get this date, but how to do I reference the file?


Solution

  • You can pass it a string of the file name.

    irb(main):001:0> File.mtime("Gemfile")
    => 2016-08-22 13:54:43 -0700
    

    To reference files from within rails you can use Rails.root.join:

    gemfile = Rails.root.join("Gemfile")
    => #<Pathname:/Users/username/projects/appname/Gemfile>
    
    File.mtime(gemfile)
    => 2016-08-22 13:54:43 -0700
    

    The docs also mention you can pass it an IO object.