-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathio.rbs
More file actions
3455 lines (3316 loc) · 114 KB
/
io.rbs
File metadata and controls
3455 lines (3316 loc) · 114 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=io.c -->
# An instance of class IO (commonly called a *stream*) represents an
# input/output stream in the underlying operating system. Class IO is the basis
# for input and output in Ruby.
#
# Class File is the only class in the Ruby core that is a subclass of IO. Some
# classes in the Ruby standard library are also subclasses of IO; these include
# TCPSocket and UDPSocket.
#
# The global constant ARGF (also accessible as <code>$<</code>) provides an
# IO-like stream that allows access to all file paths found in ARGV (or found in
# STDIN if ARGV is empty). ARGF is not itself a subclass of IO.
#
# Class StringIO provides an IO-like stream that handles a String. StringIO is
# not itself a subclass of IO.
#
# Important objects based on IO include:
#
# * $stdin.
# * $stdout.
# * $stderr.
# * Instances of class File.
#
# An instance of IO may be created using:
#
# * IO.new: returns a new IO object for the given integer file descriptor.
# * IO.open: passes a new IO object to the given block.
# * IO.popen: returns a new IO object that is connected to the $stdin and
# $stdout of a newly-launched subprocess.
# * Kernel#open: Returns a new IO object connected to a given source: stream,
# file, or subprocess.
#
# Like a File stream, an IO stream has:
#
# * A read/write mode, which may be read-only, write-only, or read/write; see
# [Read/Write Mode](rdoc-ref:File@Read-2FWrite+Mode).
# * A data mode, which may be text-only or binary; see [Data
# Mode](rdoc-ref:File@Data+Mode).
# * Internal and external encodings; see [Encodings](rdoc-ref:File@Encodings).
#
# And like other IO streams, it has:
#
# * A position, which determines where in the stream the next read or write is
# to occur; see [Position](rdoc-ref:IO@Position).
# * A line number, which is a special, line-oriented, "position" (different
# from the position mentioned above); see [Line
# Number](rdoc-ref:IO@Line+Number).
#
# ## Extension <code>io/console</code>
#
# Extension <code>io/console</code> provides numerous methods for interacting
# with the console; requiring it adds numerous methods to class IO.
#
# ## Example Files
#
# Many examples here use these variables:
#
# # English text with newlines.
# text = <<~EOT
# First line
# Second line
#
# Fourth line
# Fifth line
# EOT
#
# # Russian text.
# russian = "\u{442 435 441 442}" # => "тест"
#
# # Binary data.
# data = "\u9990\u9991\u9992\u9993\u9994"
#
# # Text file.
# File.write('t.txt', text)
#
# # File with Russian text.
# File.write('t.rus', russian)
#
# # File with binary data.
# f = File.new('t.dat', 'wb:UTF-16')
# f.write(data)
# f.close
#
# ## Open Options
#
# A number of IO methods accept optional keyword arguments that determine how a
# new stream is to be opened:
#
# * <code>:mode</code>: Stream mode.
# * <code>:flags</code>: Integer file open flags; If `mode` is also given, the
# two are bitwise-ORed.
# * <code>:external_encoding</code>: External encoding for the stream.
# * <code>:internal_encoding</code>: Internal encoding for the stream.
# <code>'-'</code> is a synonym for the default internal encoding. If the
# value is `nil` no conversion occurs.
# * <code>:encoding</code>: Specifies external and internal encodings as
# <code>'extern:intern'</code>.
# * <code>:textmode</code>: If a truthy value, specifies the mode as
# text-only, binary otherwise.
# * <code>:binmode</code>: If a truthy value, specifies the mode as binary,
# text-only otherwise.
# * <code>:autoclose</code>: If a truthy value, specifies that the `fd` will
# close when the stream closes; otherwise it remains open.
# * <code>:path:</code> If a string value is provided, it is used in #inspect
# and is available as #path method.
#
# Also available are the options offered in String#encode, which may control
# conversion between external and internal encoding.
#
# ## Basic IO
#
# You can perform basic stream IO with these methods, which typically operate on
# multi-byte strings:
#
# * IO#read: Reads and returns some or all of the remaining bytes from the
# stream.
# * IO#write: Writes zero or more strings to the stream; each given object
# that is not already a string is converted via `to_s`.
#
# ### Position
#
# An IO stream has a nonnegative integer *position*, which is the byte offset at
# which the next read or write is to occur. A new stream has position zero (and
# line number zero); method `rewind` resets the position (and line number) to
# zero.
#
# These methods discard [buffers](rdoc-ref:IO@Buffering) and the
# Encoding::Converter instances used for that IO.
#
# The relevant methods:
#
# * IO#tell (aliased as <code>#pos</code>): Returns the current position (in
# bytes) in the stream.
# * IO#pos=: Sets the position of the stream to a given integer `new_position`
# (in bytes).
# * IO#seek: Sets the position of the stream to a given integer `offset` (in
# bytes), relative to a given position `whence` (indicating the beginning,
# end, or current position).
# * IO#rewind: Positions the stream at the beginning (also resetting the line
# number).
#
# ### Open and Closed Streams
#
# A new IO stream may be open for reading, open for writing, or both.
#
# A stream is automatically closed when claimed by the garbage collector.
#
# Attempted reading or writing on a closed stream raises an exception.
#
# The relevant methods:
#
# * IO#close: Closes the stream for both reading and writing.
# * IO#close_read: Closes the stream for reading.
# * IO#close_write: Closes the stream for writing.
# * IO#closed?: Returns whether the stream is closed.
#
# ### End-of-Stream
#
# You can query whether a stream is positioned at its end:
#
# * IO#eof? (also aliased as <code>#eof</code>): Returns whether the stream is
# at end-of-stream.
#
# You can reposition to end-of-stream by using method IO#seek:
#
# f = File.new('t.txt')
# f.eof? # => false
# f.seek(0, :END)
# f.eof? # => true
# f.close
#
# Or by reading all stream content (which is slower than using IO#seek):
#
# f.rewind
# f.eof? # => false
# f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
# f.eof? # => true
#
# ## Line IO
#
# Class IO supports line-oriented [input](rdoc-ref:IO@Line+Input) and
# [output](rdoc-ref:IO@Line+Output)
#
# ### Line Input
#
# Class IO supports line-oriented input for [files](rdoc-ref:IO@File+Line+Input)
# and [IO streams](rdoc-ref:IO@Stream+Line+Input)
#
# #### File Line Input
#
# You can read lines from a file using these methods:
#
# * IO.foreach: Reads each line and passes it to the given block.
# * IO.readlines: Reads and returns all lines in an array.
#
# For each of these methods:
#
# * You can specify [open options](rdoc-ref:IO@Open+Options).
# * Line parsing depends on the effective *line separator*; see [Line
# Separator](rdoc-ref:IO@Line+Separator).
# * The length of each returned line depends on the effective *line limit*;
# see [Line Limit](rdoc-ref:IO@Line+Limit).
#
# #### Stream Line Input
#
# You can read lines from an IO stream using these methods:
#
# * IO#each_line: Reads each remaining line, passing it to the given block.
# * IO#gets: Returns the next line.
# * IO#readline: Like #gets, but raises an exception at end-of-stream.
# * IO#readlines: Returns all remaining lines in an array.
#
# For each of these methods:
#
# * Reading may begin mid-line, depending on the stream's *position*; see
# [Position](rdoc-ref:IO@Position).
# * Line parsing depends on the effective *line separator*; see [Line
# Separator](rdoc-ref:IO@Line+Separator).
# * The length of each returned line depends on the effective *line limit*;
# see [Line Limit](rdoc-ref:IO@Line+Limit).
#
# ##### Line Separator
#
# Each of the [line input methods](rdoc-ref:IO@Line+Input) uses a *line
# separator*: the string that determines what is considered a line; it is
# sometimes called the *input record separator*.
#
# The default line separator is taken from global variable <code>$/</code>,
# whose initial value is <code>"\n"</code>.
#
# Generally, the line to be read next is all data from the current
# [position](rdoc-ref:IO@Position) to the next line separator (but see [Special
# Line Separator Values](rdoc-ref:IO@Special+Line+Separator+Values)):
#
# f = File.new('t.txt')
# # Method gets with no sep argument returns the next line, according to $/.
# f.gets # => "First line\n"
# f.gets # => "Second line\n"
# f.gets # => "\n"
# f.gets # => "Fourth line\n"
# f.gets # => "Fifth line\n"
# f.close
#
# You can use a different line separator by passing argument `sep`:
#
# f = File.new('t.txt')
# f.gets('l') # => "First l"
# f.gets('li') # => "ine\nSecond li"
# f.gets('lin') # => "ne\n\nFourth lin"
# f.gets # => "e\n"
# f.close
#
# Or by setting global variable <code>$/</code>:
#
# f = File.new('t.txt')
# $/ = 'l'
# f.gets # => "First l"
# f.gets # => "ine\nSecond l"
# f.gets # => "ine\n\nFourth l"
# f.close
#
# ##### Special Line Separator Values
#
# Each of the [line input methods](rdoc-ref:IO@Line+Input) accepts two special
# values for parameter `sep`:
#
# * `nil`: The entire stream is to be read ("slurped") into a single string:
#
# f = File.new('t.txt')
# f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
# f.close
#
# * <code>''</code> (the empty string): The next "paragraph" is to be read
# (paragraphs being separated by two consecutive line separators):
#
# f = File.new('t.txt')
# f.gets('') # => "First line\nSecond line\n\n"
# f.gets('') # => "Fourth line\nFifth line\n"
# f.close
#
# ##### Line Limit
#
# Each of the [line input methods](rdoc-ref:IO@Line+Input) uses an integer *line
# limit*, which restricts the number of bytes that may be returned. (A
# multi-byte character will not be split, and so a returned line may be slightly
# longer than the limit).
#
# The default limit value is <code>-1</code>; any negative limit value means
# that there is no limit.
#
# If there is no limit, the line is determined only by `sep`.
#
# # Text with 1-byte characters.
# File.open('t.txt') {|f| f.gets(1) } # => "F"
# File.open('t.txt') {|f| f.gets(2) } # => "Fi"
# File.open('t.txt') {|f| f.gets(3) } # => "Fir"
# File.open('t.txt') {|f| f.gets(4) } # => "Firs"
# # No more than one line.
# File.open('t.txt') {|f| f.gets(10) } # => "First line"
# File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
# File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
#
# # Text with 2-byte characters, which will not be split.
# File.open('t.rus') {|f| f.gets(1).size } # => 1
# File.open('t.rus') {|f| f.gets(2).size } # => 1
# File.open('t.rus') {|f| f.gets(3).size } # => 2
# File.open('t.rus') {|f| f.gets(4).size } # => 2
#
# ##### Line Separator and Line Limit
#
# With arguments `sep` and `limit` given, combines the two behaviors:
#
# * Returns the next line as determined by line separator `sep`.
# * But returns no more bytes than are allowed by the limit `limit`.
#
# Example:
#
# File.open('t.txt') {|f| f.gets('li', 20) } # => "First li"
# File.open('t.txt') {|f| f.gets('li', 2) } # => "Fi"
#
# ##### Line Number
#
# A readable IO stream has a non-negative integer *line number*:
#
# * IO#lineno: Returns the line number.
# * IO#lineno=: Resets and returns the line number.
#
# Unless modified by a call to method IO#lineno=, the line number is the number
# of lines read by certain line-oriented methods, according to the effective
# [line separator](rdoc-ref:IO@Line+Separator):
#
# * IO.foreach: Increments the line number on each call to the block.
# * IO#each_line: Increments the line number on each call to the block.
# * IO#gets: Increments the line number.
# * IO#readline: Increments the line number.
# * IO#readlines: Increments the line number for each line read.
#
# A new stream is initially has line number zero (and position zero); method
# `rewind` resets the line number (and position) to zero:
#
# f = File.new('t.txt')
# f.lineno # => 0
# f.gets # => "First line\n"
# f.lineno # => 1
# f.rewind
# f.lineno # => 0
# f.close
#
# Reading lines from a stream usually changes its line number:
#
# f = File.new('t.txt', 'r')
# f.lineno # => 0
# f.readline # => "This is line one.\n"
# f.lineno # => 1
# f.readline # => "This is the second line.\n"
# f.lineno # => 2
# f.readline # => "Here's the third line.\n"
# f.lineno # => 3
# f.eof? # => true
# f.close
#
# Iterating over lines in a stream usually changes its line number:
#
# File.open('t.txt') do |f|
# f.each_line do |line|
# p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}"
# end
# end
#
# Output:
#
# "position=11 eof?=false lineno=1"
# "position=23 eof?=false lineno=2"
# "position=24 eof?=false lineno=3"
# "position=36 eof?=false lineno=4"
# "position=47 eof?=true lineno=5"
#
# Unlike the stream's [position](rdoc-ref:IO@Position), the line number does not
# affect where the next read or write will occur:
#
# f = File.new('t.txt')
# f.lineno = 1000
# f.lineno # => 1000
# f.gets # => "First line\n"
# f.lineno # => 1001
# f.close
#
# Associated with the line number is the global variable <code>$.</code>:
#
# * When a stream is opened, <code>$.</code> is not set; its value is left
# over from previous activity in the process:
#
# $. = 41
# f = File.new('t.txt')
# $. = 41
# # => 41
# f.close
#
# * When a stream is read, <code>$.</code> is set to the line number for that
# stream:
#
# f0 = File.new('t.txt')
# f1 = File.new('t.dat')
# f0.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
# $. # => 5
# f1.readlines # => ["\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"]
# $. # => 1
# f0.close
# f1.close
#
# * Methods IO#rewind and IO#seek do not affect <code>$.</code>:
#
# f = File.new('t.txt')
# f.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
# $. # => 5
# f.rewind
# f.seek(0, :SET)
# $. # => 5
# f.close
#
# ### Line Output
#
# You can write to an IO stream line-by-line using this method:
#
# * IO#puts: Writes objects to the stream.
#
# ## Character IO
#
# You can process an IO stream character-by-character using these methods:
#
# * IO#getc: Reads and returns the next character from the stream.
# * IO#readchar: Like #getc, but raises an exception at end-of-stream.
# * IO#ungetc: Pushes back ("unshifts") a character or integer onto the
# stream.
# * IO#putc: Writes a character to the stream.
# * IO#each_char: Reads each remaining character in the stream, passing the
# character to the given block.
#
# ## Byte IO
#
# You can process an IO stream byte-by-byte using these methods:
#
# * IO#getbyte: Returns the next 8-bit byte as an integer in range 0..255.
# * IO#readbyte: Like #getbyte, but raises an exception if at end-of-stream.
# * IO#ungetbyte: Pushes back ("unshifts") a byte back onto the stream.
# * IO#each_byte: Reads each remaining byte in the stream, passing the byte to
# the given block.
#
# ## Codepoint IO
#
# You can process an IO stream codepoint-by-codepoint:
#
# * IO#each_codepoint: Reads each remaining codepoint, passing it to the given
# block.
#
# ## What's Here
#
# First, what's elsewhere. Class IO:
#
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
# provides dozens of additional methods.
#
# Here, class IO provides methods that are useful for:
#
# * [Creating](rdoc-ref:IO@Creating)
# * [Reading](rdoc-ref:IO@Reading)
# * [Writing](rdoc-ref:IO@Writing)
# * [Positioning](rdoc-ref:IO@Positioning)
# * [Iterating](rdoc-ref:IO@Iterating)
# * [Settings](rdoc-ref:IO@Settings)
# * [Querying](rdoc-ref:IO@Querying)
# * [Buffering](rdoc-ref:IO@Buffering)
# * [Low-Level Access](rdoc-ref:IO@Low-Level+Access)
# * [Other](rdoc-ref:IO@Other)
#
# ### Creating
#
# * ::new (aliased as ::for_fd): Creates and returns a new IO object for the
# given integer file descriptor.
# * ::open: Creates a new IO object.
# * ::pipe: Creates a connected pair of reader and writer IO objects.
# * ::popen: Creates an IO object to interact with a subprocess.
# * ::select: Selects which given IO instances are ready for reading, writing,
# or have pending exceptions.
#
# ### Reading
#
# * ::binread: Returns a binary string with all or a subset of bytes from the
# given file.
# * ::read: Returns a string with all or a subset of bytes from the given
# file.
# * ::readlines: Returns an array of strings, which are the lines from the
# given file.
# * #getbyte: Returns the next 8-bit byte read from `self` as an integer.
# * #getc: Returns the next character read from `self` as a string.
# * #gets: Returns the line read from `self`.
# * #pread: Returns all or the next *n* bytes read from `self`, not updating
# the receiver's offset.
# * #read: Returns all remaining or the next *n* bytes read from `self` for a
# given *n*.
# * #read_nonblock: the next *n* bytes read from `self` for a given *n*, in
# non-block mode.
# * #readbyte: Returns the next byte read from `self`; same as #getbyte, but
# raises an exception on end-of-stream.
# * #readchar: Returns the next character read from `self`; same as #getc, but
# raises an exception on end-of-stream.
# * #readline: Returns the next line read from `self`; same as #getline, but
# raises an exception of end-of-stream.
# * #readlines: Returns an array of all lines read read from `self`.
# * #readpartial: Returns up to the given number of bytes from `self`.
#
# ### Writing
#
# * ::binwrite: Writes the given string to the file at the given filepath, in
# binary mode.
# * ::write: Writes the given string to `self`.
# * #<<: Appends the given string to `self`.
# * #print: Prints last read line or given objects to `self`.
# * #printf: Writes to `self` based on the given format string and objects.
# * #putc: Writes a character to `self`.
# * #puts: Writes lines to `self`, making sure line ends with a newline.
# * #pwrite: Writes the given string at the given offset, not updating the
# receiver's offset.
# * #write: Writes one or more given strings to `self`.
# * #write_nonblock: Writes one or more given strings to `self` in
# non-blocking mode.
#
# ### Positioning
#
# * #lineno: Returns the current line number in `self`.
# * #lineno=: Sets the line number is `self`.
# * #pos (aliased as #tell): Returns the current byte offset in `self`.
# * #pos=: Sets the byte offset in `self`.
# * #reopen: Reassociates `self` with a new or existing IO stream.
# * #rewind: Positions `self` to the beginning of input.
# * #seek: Sets the offset for `self` relative to given position.
#
# ### Iterating
#
# * ::foreach: Yields each line of given file to the block.
# * #each (aliased as #each_line): Calls the given block with each successive
# line in `self`.
# * #each_byte: Calls the given block with each successive byte in `self` as
# an integer.
# * #each_char: Calls the given block with each successive character in `self`
# as a string.
# * #each_codepoint: Calls the given block with each successive codepoint in
# `self` as an integer.
#
# ### Settings
#
# * #autoclose=: Sets whether `self` auto-closes.
# * #binmode: Sets `self` to binary mode.
# * #close: Closes `self`.
# * #close_on_exec=: Sets the close-on-exec flag.
# * #close_read: Closes `self` for reading.
# * #close_write: Closes `self` for writing.
# * #set_encoding: Sets the encoding for `self`.
# * #set_encoding_by_bom: Sets the encoding for `self`, based on its Unicode
# byte-order-mark.
# * #sync=: Sets the sync-mode to the given value.
#
# ### Querying
#
# * #autoclose?: Returns whether `self` auto-closes.
# * #binmode?: Returns whether `self` is in binary mode.
# * #close_on_exec?: Returns the close-on-exec flag for `self`.
# * #closed?: Returns whether `self` is closed.
# * #eof? (aliased as #eof): Returns whether `self` is at end-of-stream.
# * #external_encoding: Returns the external encoding object for `self`.
# * #fileno (aliased as #to_i): Returns the integer file descriptor for `self`
# * #internal_encoding: Returns the internal encoding object for `self`.
# * #pid: Returns the process ID of a child process associated with `self`, if
# `self` was created by ::popen.
# * #stat: Returns the File::Stat object containing status information for
# `self`.
# * #sync: Returns whether `self` is in sync-mode.
# * #tty? (aliased as #isatty): Returns whether `self` is a terminal.
#
# ### Buffering
#
# * #fdatasync: Immediately writes all buffered data in `self` to disk.
# * #flush: Flushes any buffered data within `self` to the underlying
# operating system.
# * #fsync: Immediately writes all buffered data and attributes in `self` to
# disk.
# * #ungetbyte: Prepends buffer for `self` with given integer byte or string.
# * #ungetc: Prepends buffer for `self` with given string.
#
# ### Low-Level Access
#
# * ::sysopen: Opens the file given by its path, returning the integer file
# descriptor.
# * #advise: Announces the intention to access data from `self` in a specific
# way.
# * #fcntl: Passes a low-level command to the file specified by the given file
# descriptor.
# * #ioctl: Passes a low-level command to the device specified by the given
# file descriptor.
# * #sysread: Returns up to the next *n* bytes read from self using a
# low-level read.
# * #sysseek: Sets the offset for `self`.
# * #syswrite: Writes the given string to `self` using a low-level write.
#
# ### Other
#
# * ::copy_stream: Copies data from a source to a destination, each of which
# is a filepath or an IO-like object.
# * ::try_convert: Returns a new IO object resulting from converting the given
# object.
# * #inspect: Returns the string representation of `self`.
#
%a{annotate:rdoc:source:from=io.c}
class IO < Object
include File::Constants
include Enumerable[String]
# <!--
# rdoc-file=io.c
# - self << object -> self
# -->
# Writes the given `object` to `self`, which must be opened for writing (see
# [Access Modes](rdoc-ref:File@Access+Modes)); returns `self`; if `object` is
# not a string, it is converted via method `to_s`:
#
# $stdout << 'Hello' << ', ' << 'World!' << "\n"
# $stdout << 'foo' << :bar << 2 << "\n"
#
# Output:
#
# Hello, World!
# foobar2
#
def <<: (_ToS obj) -> self
# <!--
# rdoc-file=io.c
# - advise(advice, offset = 0, len = 0) -> nil
# -->
# Invokes Posix system call
# [posix_fadvise(2)](https://linux.die.net/man/2/posix_fadvise), which announces
# an intention to access data from the current file in a particular manner.
#
# The arguments and results are platform-dependent.
#
# The relevant data is specified by:
#
# * `offset`: The offset of the first byte of data.
# * `len`: The number of bytes to be accessed; if `len` is zero, or is larger
# than the number of bytes remaining, all remaining bytes will be accessed.
#
# Argument `advice` is one of the following symbols:
#
# * <code>:normal</code>: The application has no advice to give about its
# access pattern for the specified data. If no advice is given for an open
# file, this is the default assumption.
# * <code>:sequential</code>: The application expects to access the specified
# data sequentially (with lower offsets read before higher ones).
# * <code>:random</code>: The specified data will be accessed in random order.
# * <code>:noreuse</code>: The specified data will be accessed only once.
# * <code>:willneed</code>: The specified data will be accessed in the near
# future.
# * <code>:dontneed</code>: The specified data will not be accessed in the
# near future.
#
# Not implemented on all platforms.
#
def advise: (:normal | :sequential | :random | :willneed | :dontneed | :noreuse advise, ?Integer offset, ?Integer len) -> nil
# <!--
# rdoc-file=io.c
# - io.autoclose = bool -> true or false
# -->
# Sets auto-close flag.
#
# f = File.open(File::NULL)
# IO.for_fd(f.fileno).close
# f.gets # raises Errno::EBADF
#
# f = File.open(File::NULL)
# g = IO.for_fd(f.fileno)
# g.autoclose = false
# g.close
# f.gets # won't cause Errno::EBADF
#
def autoclose=: (boolish bool) -> boolish
# <!--
# rdoc-file=io.c
# - ios.autoclose? -> true or false
# -->
# Returns `true` if the underlying file descriptor of *ios* will be closed at
# its finalization or at calling #close, otherwise `false`.
#
def autoclose?: () -> bool
# <!--
# rdoc-file=io.c
# - binmode -> self
# -->
# Sets the stream's data mode as binary (see [Data
# Mode](rdoc-ref:File@Data+Mode)).
#
# A stream's data mode may not be changed from binary to text.
#
def binmode: () -> self
# <!--
# rdoc-file=io.c
# - binmode? -> true or false
# -->
# Returns `true` if the stream is on binary mode, `false` otherwise. See [Data
# Mode](rdoc-ref:File@Data+Mode).
#
def binmode?: () -> bool
# <!--
# rdoc-file=io.c
# - close -> nil
# -->
# Closes the stream for both reading and writing if open for either or both;
# returns `nil`. See [Open and Closed
# Streams](rdoc-ref:IO@Open+and+Closed+Streams).
#
# If the stream is open for writing, flushes any buffered writes to the
# operating system before closing.
#
# If the stream was opened by IO.popen, sets global variable <code>$?</code>
# (child exit status).
#
# It is not an error to close an IO object that has already been closed. It just
# returns nil.
#
# Example:
#
# IO.popen('ruby', 'r+') do |pipe|
# puts pipe.closed?
# pipe.close
# puts $?
# puts pipe.closed?
# end
#
# Output:
#
# false
# pid 13760 exit 0
# true
#
# Related: IO#close_read, IO#close_write, IO#closed?.
#
def close: () -> nil
# <!--
# rdoc-file=io.c
# - self.close_on_exec = bool -> true or false
# -->
# Sets a close-on-exec flag.
#
# f = File.open(File::NULL)
# f.close_on_exec = true
# system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
# f.closed? #=> false
#
# Ruby sets close-on-exec flags of all file descriptors by default since Ruby
# 2.0.0. So you don't need to set by yourself. Also, unsetting a close-on-exec
# flag can cause file descriptor leak if another thread use fork() and exec()
# (via system() method for example). If you really needs file descriptor
# inheritance to child process, use spawn()'s argument such as fd=>fd.
#
def close_on_exec=: (boolish bool) -> nil
# <!--
# rdoc-file=io.c
# - close_on_exec? -> true or false
# -->
# Returns `true` if the stream will be closed on exec, `false` otherwise:
#
# f = File.open('t.txt')
# f.close_on_exec? # => true
# f.close_on_exec = false
# f.close_on_exec? # => false
# f.close
#
def close_on_exec?: () -> bool
# <!--
# rdoc-file=io.c
# - close_read -> nil
# -->
# Closes the stream for reading if open for reading; returns `nil`. See [Open
# and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
#
# If the stream was opened by IO.popen and is also closed for writing, sets
# global variable <code>$?</code> (child exit status).
#
# Example:
#
# IO.popen('ruby', 'r+') do |pipe|
# puts pipe.closed?
# pipe.close_write
# puts pipe.closed?
# pipe.close_read
# puts $?
# puts pipe.closed?
# end
#
# Output:
#
# false
# false
# pid 14748 exit 0
# true
#
# Related: IO#close, IO#close_write, IO#closed?.
#
def close_read: () -> nil
# <!--
# rdoc-file=io.c
# - close_write -> nil
# -->
# Closes the stream for writing if open for writing; returns `nil`. See [Open
# and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
#
# Flushes any buffered writes to the operating system before closing.
#
# If the stream was opened by IO.popen and is also closed for reading, sets
# global variable <code>$?</code> (child exit status).
#
# IO.popen('ruby', 'r+') do |pipe|
# puts pipe.closed?
# pipe.close_read
# puts pipe.closed?
# pipe.close_write
# puts $?
# puts pipe.closed?
# end
#
# Output:
#
# false
# false
# pid 15044 exit 0
# true
#
# Related: IO#close, IO#close_read, IO#closed?.
#
def close_write: () -> nil
# <!--
# rdoc-file=io.c
# - closed? -> true or false
# -->
# Returns `true` if the stream is closed for both reading and writing, `false`
# otherwise. See [Open and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
#
# IO.popen('ruby', 'r+') do |pipe|
# puts pipe.closed?
# pipe.close_read
# puts pipe.closed?
# pipe.close_write
# puts pipe.closed?
# end
#
# Output:
#
# false
# false
# true
#
# Related: IO#close_read, IO#close_write, IO#close.
#
def closed?: () -> bool
# <!--
# rdoc-file=io.c
# - each_byte {|byte| ... } -> self
# - each_byte -> enumerator
# -->
# Calls the given block with each byte (0..255) in the stream; returns `self`.
# See [Byte IO](rdoc-ref:IO@Byte+IO).
#
# f = File.new('t.rus')
# a = []
# f.each_byte {|b| a << b }
# a # => [209, 130, 208, 181, 209, 129, 209, 130]
# f.close
#
# Returns an Enumerator if no block is given.
#
# Related: IO#each_char, IO#each_codepoint.
#
def each_byte: () { (Integer byte) -> void } -> self
| () -> ::Enumerator[Integer, self]
# <!--
# rdoc-file=io.c
# - each_char {|c| ... } -> self
# - each_char -> enumerator
# -->
# Calls the given block with each character in the stream; returns `self`. See
# [Character IO](rdoc-ref:IO@Character+IO).
#
# f = File.new('t.rus')
# a = []
# f.each_char {|c| a << c.ord }
# a # => [1090, 1077, 1089, 1090]
# f.close
#
# Returns an Enumerator if no block is given.
#
# Related: IO#each_byte, IO#each_codepoint.
#
def each_char: () { (String c) -> void } -> self
| () -> ::Enumerator[String, self]
# <!--
# rdoc-file=io.c
# - each_codepoint {|c| ... } -> self
# - each_codepoint -> enumerator
# -->
# Calls the given block with each codepoint in the stream; returns `self`:
#
# f = File.new('t.rus')
# a = []
# f.each_codepoint {|c| a << c }
# a # => [1090, 1077, 1089, 1090]
# f.close
#
# Returns an Enumerator if no block is given.
#
# Related: IO#each_byte, IO#each_char.
#
def each_codepoint: () { (Integer c) -> void } -> self
| () -> ::Enumerator[Integer, self]
# <!--
# rdoc-file=io.c
# - eof -> true or false
# -->
# Returns `true` if the stream is positioned at its end, `false` otherwise; see
# [Position](rdoc-ref:IO@Position):
#
# f = File.open('t.txt')
# f.eof # => false
# f.seek(0, :END) # => 0
# f.eof # => true
# f.close
#
# Raises an exception unless the stream is opened for reading; see
# [Mode](rdoc-ref:File@Access+Modes).
#
# If `self` is a stream such as pipe or socket, this method blocks until the
# other end sends some data or closes it:
#
# r, w = IO.pipe
# Thread.new { sleep 1; w.close }
# r.eof? # => true # After 1-second wait.
#
# r, w = IO.pipe
# Thread.new { sleep 1; w.puts "a" }
# r.eof? # => false # After 1-second wait.
#
# r, w = IO.pipe
# r.eof? # blocks forever
#
# Note that this method reads data to the input byte buffer. So IO#sysread may
# not behave as you intend with IO#eof?, unless you call IO#rewind first (which
# is not available for some streams).
#
def eof: () -> bool
# <!--
# rdoc-file=io.c
# - fcntl(integer_cmd, argument) -> integer
# -->
# Invokes Posix system call [fcntl(2)](https://linux.die.net/man/2/fcntl), which
# provides a mechanism for issuing low-level commands to control or query a
# file-oriented I/O stream. Arguments and results are platform dependent.
#
# If `argument` is a number, its value is passed directly; if it is a string, it
# is interpreted as a binary sequence of bytes. (Array#pack might be a useful
# way to build this string.)
#
# Not implemented on all platforms.
#
def fcntl: (Integer integer_cmd, String | Integer argument) -> Integer
# <!--
# rdoc-file=io.c
# - fdatasync -> 0
# -->
# Immediately writes to disk all data buffered in the stream, via the operating
# system's: <code>fdatasync(2)</code>, if supported, otherwise via
# <code>fsync(2)</code>, if supported; otherwise raises an exception.
#
def fdatasync: () -> Integer?