Skip to content

Commit 4245f8e

Browse files
[DOC] Harmonize ::atime and #atime methods (ruby#16620)
1 parent 7f4db64 commit 4245f8e

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

file.c

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,12 +1101,26 @@ static VALUE statx_birthtime(const rb_io_stat_data *st);
11011101

11021102
/*
11031103
* call-seq:
1104-
* stat.atime -> time
1105-
*
1106-
* Returns the last access time for this file as an object of class
1107-
* Time.
1108-
*
1109-
* File.stat("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
1104+
* atime -> new_time
1105+
*
1106+
* Returns a new Time object containing the access time
1107+
* of the object represented by +self+
1108+
* at the time +self+ was created;
1109+
* see {Snapshot}[rdoc-ref:File::Stat@Snapshot]:
1110+
*
1111+
* filepath = 't.tmp'
1112+
* File.write(filepath, 'foo')
1113+
* file = File.new(filepath, 'w')
1114+
* stat = File::Stat.new(filepath)
1115+
* file.atime # => 2026-03-31 16:26:39.5913207 -0500
1116+
* stat.atime # => 2026-03-31 16:26:39.5913207 -0500
1117+
* File.write(filepath, 'bar')
1118+
* file.atime # => 2026-03-31 16:27:01.4981624 -0500 # Changed by access.
1119+
* stat.atime # => 2026-03-31 16:26:39.5913207 -0500 # Unchanged by access.
1120+
* stat = File::Stat.new(filepath)
1121+
* stat.atime # => 2026-03-31 16:27:01.4981624 -0500 # New access time.
1122+
* file.close
1123+
* File.delete(filepath)
11101124
*
11111125
*/
11121126

@@ -2452,13 +2466,23 @@ rb_file_s_ftype(VALUE klass, VALUE fname)
24522466

24532467
/*
24542468
* call-seq:
2455-
* File.atime(file_name) -> time
2469+
* File.atime(object) -> new_time
24562470
*
2457-
* Returns the last access time for the named file as a Time object.
2471+
* Returns a new Time object containing the time of the most recent
2472+
* access (read or write) to the object,
2473+
* which may be a string filepath or dirpath, or a File or Dir object:
24582474
*
2459-
* _file_name_ can be an IO object.
2475+
* filepath = 't.tmp'
2476+
* File.exist?(filepath) # => false
2477+
* File.atime(filepath) # Raises Errno::ENOENT.
2478+
* File.write(filepath, 'foo')
2479+
* File.atime(filepath) # => 2026-03-31 16:39:37.9290772 -0500
2480+
* File.write(filepath, 'bar')
2481+
* File.atime(filepath) # => 2026-03-31 16:39:57.7710876 -0500
24602482
*
2461-
* File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
2483+
* File.atime('.') # => 2026-03-31 16:47:49.0970483 -0500
2484+
* File.atime(File.new('README.md')) # => 2026-03-31 11:15:27.8215934 -0500
2485+
* File.atime(Dir.new('.')) # => 2026-03-31 12:39:45.5910591 -0500
24622486
*
24632487
*/
24642488

@@ -2477,12 +2501,20 @@ rb_file_s_atime(VALUE klass, VALUE fname)
24772501

24782502
/*
24792503
* call-seq:
2480-
* file.atime -> time
2481-
*
2482-
* Returns the last access time (a Time object) for <i>file</i>, or
2483-
* epoch if <i>file</i> has not been accessed.
2484-
*
2485-
* File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
2504+
* atime -> new_time
2505+
*
2506+
* Returns a new Time object containing the time of the most recent
2507+
* access (read or write) to the file represented by +self+:
2508+
*
2509+
* filepath = 't.tmp'
2510+
* file = File.new(filepath, 'a+')
2511+
* file.atime # => 2026-03-31 17:11:27.7285397 -0500
2512+
* file.write('foo')
2513+
* file.atime # => 2026-03-31 17:11:27.7285397 -0500 # Unchanged; not yet written.
2514+
* file.flush
2515+
* file.atime # => 2026-03-31 17:12:11.3408054 -0500 # Changed; now written.
2516+
* file.close
2517+
* File.delete(filename)
24862518
*
24872519
*/
24882520

pathname_builtin.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,15 +1066,25 @@ def binwrite(...) File.binwrite(@path, ...) end
10661066
# atime -> new_time
10671067
#
10681068
# Returns a new Time object containing the time of the most recent
1069-
# access (read or write) to the entry;
1070-
# via File.atime:
1071-
#
1072-
# pn = Pathname.new('t.tmp')
1073-
# pn.write('foo')
1074-
# pn.atime # => 2026-03-22 13:49:44.5165608 -0500
1075-
# pn.read # => "foo"
1076-
# pn.atime # => 2026-03-22 13:49:57.5359349 -0500
1077-
# pn.delete
1069+
# access (read or write) to the entry represented by +self+:
1070+
#
1071+
# filepath = 't.tmp'
1072+
# pn = Pathname.new(filepath)
1073+
# File.exist?(filepath) # => false
1074+
# pn.atime # Raises Errno::ENOENT: No such file or directory
1075+
# File.write(filepath, 'foo')
1076+
# pn.atime # => 2026-03-22 13:49:44.5165608 -0500
1077+
# File.read(filepath)
1078+
# pn.atime # => 2026-03-22 13:49:57.5359349 -0500
1079+
# File.delete(filepath)
1080+
#
1081+
# dirpath = 'tmp'
1082+
# Dir.mkdir(dirpath)
1083+
# pn = Pathname.new(dirpath)
1084+
# pn.atime # => 2026-03-31 11:46:35.4813492 -0500
1085+
# Dir.empty?(dirname) # => true
1086+
# pn.atime # => 2026-03-31 11:51:10.1210092 -0500
1087+
# Dir.delete(dirpath)
10781088
#
10791089
def atime() File.atime(@path) end
10801090

0 commit comments

Comments
 (0)