2

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?

Boss Nass
  • 3,384
  • 9
  • 48
  • 90

2 Answers2

4

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.

codyeatworld
  • 1,263
  • 7
  • 9
1

Try like this

File.mtime("/home/user/path/file.rb")
Keval
  • 3,246
  • 20
  • 28