Is it possible to read a file's modification date with Ruby?

Ruby

Ruby Problem Overview


Is it possible to read a file's modification date with Ruby? I have successfully opened a text file and captured the contents of the file with

File.open("test.txt", "r").each do |line|"

but it would be very useful to read the modification date of the file.

Ruby Solutions


Solution 1 - Ruby

Use mtime:

File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300

Solution 2 - Ruby

"Returns the modification time for the named file as a Time object."

File.mtime("testfile")   #=> Tue Apr 08 12:58:04 CDT 2003

Check this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionryan1393402View Question on Stackoverflow
Solution 1 - RubyConstantine MView Answer on Stackoverflow
Solution 2 - RubyMatziView Answer on Stackoverflow