rbs 3.3.2 → 3.4.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -5
  3. data/.github/workflows/ruby.yml +7 -8
  4. data/.github/workflows/typecheck.yml +37 -0
  5. data/CHANGELOG.md +50 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +11 -11
  8. data/Steepfile +2 -2
  9. data/core/array.rbs +19 -49
  10. data/core/basic_object.rbs +2 -2
  11. data/core/comparable.rbs +17 -8
  12. data/core/complex.rbs +82 -43
  13. data/core/data.rbs +2 -4
  14. data/core/dir.rbs +635 -295
  15. data/core/enumerable.rbs +11 -18
  16. data/core/enumerator.rbs +37 -31
  17. data/core/errors.rbs +4 -0
  18. data/core/false_class.rbs +34 -15
  19. data/core/fiber.rbs +23 -0
  20. data/core/file.rbs +329 -120
  21. data/core/float.rbs +17 -32
  22. data/core/gc.rbs +17 -11
  23. data/core/hash.rbs +22 -44
  24. data/core/integer.rbs +82 -113
  25. data/core/io/buffer.rbs +90 -47
  26. data/core/io.rbs +39 -116
  27. data/core/kernel.rbs +442 -489
  28. data/core/match_data.rbs +55 -56
  29. data/core/module.rbs +45 -1
  30. data/core/nil_class.rbs +98 -35
  31. data/core/numeric.rbs +22 -32
  32. data/core/object_space/weak_key_map.rbs +102 -0
  33. data/core/process.rbs +1242 -655
  34. data/core/ractor.rbs +139 -120
  35. data/core/range.rbs +100 -4
  36. data/core/rational.rbs +0 -4
  37. data/core/rbs/unnamed/argf.rbs +16 -8
  38. data/core/rbs/unnamed/env_class.rbs +0 -24
  39. data/core/refinement.rbs +8 -0
  40. data/core/regexp.rbs +1149 -598
  41. data/core/ruby_vm.rbs +126 -12
  42. data/core/rubygems/platform.rbs +9 -0
  43. data/core/rubygems/rubygems.rbs +1 -1
  44. data/core/rubygems/version.rbs +5 -1
  45. data/core/set.rbs +20 -22
  46. data/core/signal.rbs +4 -4
  47. data/core/string.rbs +283 -230
  48. data/core/string_io.rbs +2 -14
  49. data/core/struct.rbs +404 -24
  50. data/core/symbol.rbs +1 -19
  51. data/core/thread.rbs +29 -12
  52. data/core/time.rbs +227 -104
  53. data/core/trace_point.rbs +2 -5
  54. data/core/true_class.rbs +54 -21
  55. data/core/warning.rbs +14 -11
  56. data/docs/data_and_struct.md +29 -0
  57. data/docs/syntax.md +3 -5
  58. data/docs/tools.md +1 -0
  59. data/ext/rbs_extension/lexer.c +643 -559
  60. data/ext/rbs_extension/lexer.re +5 -1
  61. data/ext/rbs_extension/parser.c +12 -3
  62. data/ext/rbs_extension/unescape.c +7 -47
  63. data/lib/rbs/cli/diff.rb +4 -1
  64. data/lib/rbs/cli/validate.rb +280 -0
  65. data/lib/rbs/cli.rb +2 -194
  66. data/lib/rbs/collection/config.rb +5 -6
  67. data/lib/rbs/collection/sources/git.rb +1 -1
  68. data/lib/rbs/collection.rb +1 -0
  69. data/lib/rbs/diff.rb +7 -4
  70. data/lib/rbs/errors.rb +11 -0
  71. data/lib/rbs/test/errors.rb +4 -1
  72. data/lib/rbs/test/guaranteed.rb +2 -3
  73. data/lib/rbs/test/type_check.rb +15 -10
  74. data/lib/rbs/test.rb +3 -3
  75. data/lib/rbs/types.rb +29 -0
  76. data/lib/rbs/unit_test/convertibles.rb +176 -0
  77. data/lib/rbs/unit_test/spy.rb +136 -0
  78. data/lib/rbs/unit_test/type_assertions.rb +341 -0
  79. data/lib/rbs/unit_test/with_aliases.rb +143 -0
  80. data/lib/rbs/unit_test.rb +6 -0
  81. data/lib/rbs/version.rb +1 -1
  82. data/sig/cli/validate.rbs +43 -0
  83. data/sig/diff.rbs +3 -1
  84. data/sig/errors.rbs +8 -0
  85. data/sig/rbs.rbs +1 -1
  86. data/sig/test/errors.rbs +52 -0
  87. data/sig/test/guranteed.rbs +9 -0
  88. data/sig/test/type_check.rbs +19 -0
  89. data/sig/test.rbs +82 -0
  90. data/sig/types.rbs +6 -1
  91. data/sig/unit_test/convertibles.rbs +154 -0
  92. data/sig/unit_test/spy.rbs +28 -0
  93. data/sig/unit_test/type_assertions.rbs +194 -0
  94. data/sig/unit_test/with_aliases.rbs +136 -0
  95. data/stdlib/base64/0/base64.rbs +307 -45
  96. data/stdlib/bigdecimal/0/big_decimal.rbs +35 -15
  97. data/stdlib/coverage/0/coverage.rbs +2 -2
  98. data/stdlib/csv/0/csv.rbs +25 -55
  99. data/stdlib/date/0/date.rbs +1 -43
  100. data/stdlib/date/0/date_time.rbs +1 -13
  101. data/stdlib/delegate/0/delegator.rbs +186 -0
  102. data/stdlib/delegate/0/kernel.rbs +47 -0
  103. data/stdlib/delegate/0/simple_delegator.rbs +98 -0
  104. data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -1
  105. data/stdlib/erb/0/erb.rbs +2 -2
  106. data/stdlib/fileutils/0/fileutils.rbs +0 -19
  107. data/stdlib/io-console/0/io-console.rbs +12 -1
  108. data/stdlib/ipaddr/0/ipaddr.rbs +2 -1
  109. data/stdlib/json/0/json.rbs +320 -81
  110. data/stdlib/logger/0/logger.rbs +9 -5
  111. data/stdlib/monitor/0/monitor.rbs +78 -0
  112. data/stdlib/net-http/0/net-http.rbs +1880 -543
  113. data/stdlib/objspace/0/objspace.rbs +19 -13
  114. data/stdlib/openssl/0/openssl.rbs +508 -127
  115. data/stdlib/optparse/0/optparse.rbs +25 -11
  116. data/stdlib/pathname/0/pathname.rbs +1 -1
  117. data/stdlib/pp/0/pp.rbs +2 -5
  118. data/stdlib/prettyprint/0/prettyprint.rbs +2 -2
  119. data/stdlib/pstore/0/pstore.rbs +2 -4
  120. data/stdlib/rdoc/0/comment.rbs +1 -2
  121. data/stdlib/resolv/0/resolv.rbs +4 -2
  122. data/stdlib/socket/0/socket.rbs +2 -2
  123. data/stdlib/socket/0/unix_socket.rbs +2 -2
  124. data/stdlib/strscan/0/string_scanner.rbs +3 -2
  125. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  126. data/stdlib/uri/0/common.rbs +245 -123
  127. metadata +23 -4
  128. data/lib/rbs/test/spy.rb +0 -6
data/core/integer.rbs CHANGED
@@ -131,7 +131,7 @@ class Integer < Numeric
131
131
  def self.sqrt: (int n) -> Integer
132
132
 
133
133
  # <!--
134
- # rdoc-file=numeric.rb
134
+ # rdoc-file=numeric.c
135
135
  # - Integer.try_convert(object) -> object, integer, or nil
136
136
  # -->
137
137
  # If `object` is an Integer object, returns `object`.
@@ -178,8 +178,6 @@ class Integer < Numeric
178
178
  # 10 % 3.0 # => 1.0
179
179
  # 10 % Rational(3, 1) # => (1/1)
180
180
  #
181
- # Integer#modulo is an alias for Integer#%.
182
- #
183
181
  def %: (Float) -> Float
184
182
  | (Rational) -> Rational
185
183
  | (Integer) -> Integer
@@ -277,9 +275,9 @@ class Integer < Numeric
277
275
 
278
276
  # <!--
279
277
  # rdoc-file=numeric.rb
280
- # - -int -> integer
278
+ # - -int -> integer
281
279
  # -->
282
- # Returns `int`, negated.
280
+ # Returns `self`, negated.
283
281
  #
284
282
  def -@: () -> Integer
285
283
 
@@ -391,8 +389,6 @@ class Integer < Numeric
391
389
  #
392
390
  # Related: Integer#eql? (requires `other` to be an Integer).
393
391
  #
394
- # Integer#=== is an alias for Integer#==.
395
- #
396
392
  def ==: (untyped) -> bool
397
393
 
398
394
  # <!--
@@ -406,8 +402,6 @@ class Integer < Numeric
406
402
  #
407
403
  # Related: Integer#eql? (requires `other` to be an Integer).
408
404
  #
409
- # Integer#=== is an alias for Integer#==.
410
- #
411
405
  def ===: (untyped) -> bool
412
406
 
413
407
  # <!--
@@ -519,16 +513,13 @@ class Integer < Numeric
519
513
 
520
514
  # <!--
521
515
  # rdoc-file=numeric.rb
522
- # - int.abs -> integer
523
- # - int.magnitude -> integer
516
+ # - abs -> integer
524
517
  # -->
525
- # Returns the absolute value of `int`.
518
+ # Returns the absolute value of `self`.
526
519
  #
527
- # (-12345).abs #=> 12345
528
- # -12345.abs #=> 12345
529
- # 12345.abs #=> 12345
530
- #
531
- # Integer#magnitude is an alias for Integer#abs.
520
+ # (-12345).abs # => 12345
521
+ # -12345.abs # => 12345
522
+ # 12345.abs # => 12345
532
523
  #
533
524
  def abs: () -> Integer
534
525
 
@@ -586,44 +577,43 @@ class Integer < Numeric
586
577
 
587
578
  # <!--
588
579
  # rdoc-file=numeric.rb
589
- # - int.bit_length -> integer
580
+ # - bit_length -> integer
590
581
  # -->
591
- # Returns the number of bits of the value of `int`.
592
- #
593
- # "Number of bits" means the bit position of the highest bit which is different
594
- # from the sign bit (where the least significant bit has bit position 1). If
595
- # there is no such bit (zero or minus one), zero is returned.
596
- #
597
- # I.e. this method returns *ceil(log2(int < 0 ? -int : int+1))*.
598
- #
599
- # (-2**1000-1).bit_length #=> 1001
600
- # (-2**1000).bit_length #=> 1000
601
- # (-2**1000+1).bit_length #=> 1000
602
- # (-2**12-1).bit_length #=> 13
603
- # (-2**12).bit_length #=> 12
604
- # (-2**12+1).bit_length #=> 12
605
- # -0x101.bit_length #=> 9
606
- # -0x100.bit_length #=> 8
607
- # -0xff.bit_length #=> 8
608
- # -2.bit_length #=> 1
609
- # -1.bit_length #=> 0
610
- # 0.bit_length #=> 0
611
- # 1.bit_length #=> 1
612
- # 0xff.bit_length #=> 8
613
- # 0x100.bit_length #=> 9
614
- # (2**12-1).bit_length #=> 12
615
- # (2**12).bit_length #=> 13
616
- # (2**12+1).bit_length #=> 13
617
- # (2**1000-1).bit_length #=> 1000
618
- # (2**1000).bit_length #=> 1001
619
- # (2**1000+1).bit_length #=> 1001
620
- #
621
- # This method can be used to detect overflow in Array#pack as follows:
582
+ # Returns the number of bits of the value of `self`, which is the bit position
583
+ # of the highest-order bit that is different from the sign bit (where the least
584
+ # significant bit has bit position 1). If there is no such bit (zero or minus
585
+ # one), returns zero.
586
+ #
587
+ # This method returns `ceil(log2(self < 0 ? -self : self + 1))`>.
588
+ #
589
+ # (-2**1000-1).bit_length # => 1001
590
+ # (-2**1000).bit_length # => 1000
591
+ # (-2**1000+1).bit_length # => 1000
592
+ # (-2**12-1).bit_length # => 13
593
+ # (-2**12).bit_length # => 12
594
+ # (-2**12+1).bit_length # => 12
595
+ # -0x101.bit_length # => 9
596
+ # -0x100.bit_length # => 8
597
+ # -0xff.bit_length # => 8
598
+ # -2.bit_length # => 1
599
+ # -1.bit_length # => 0
600
+ # 0.bit_length # => 0
601
+ # 1.bit_length # => 1
602
+ # 0xff.bit_length # => 8
603
+ # 0x100.bit_length # => 9
604
+ # (2**12-1).bit_length # => 12
605
+ # (2**12).bit_length # => 13
606
+ # (2**12+1).bit_length # => 13
607
+ # (2**1000-1).bit_length # => 1000
608
+ # (2**1000).bit_length # => 1001
609
+ # (2**1000+1).bit_length # => 1001
610
+ #
611
+ # For Integer *n*, this method can be used to detect overflow in Array#pack:
622
612
  #
623
613
  # if n.bit_length < 32
624
- # [n].pack("l") # no overflow
614
+ # [n].pack('l') # No overflow.
625
615
  # else
626
- # raise "overflow"
616
+ # raise 'Overflow'
627
617
  # end
628
618
  #
629
619
  def bit_length: () -> Integer
@@ -655,16 +645,16 @@ class Integer < Numeric
655
645
 
656
646
  # <!--
657
647
  # rdoc-file=numeric.rb
658
- # - ceildiv(other) -> integer
648
+ # - ceildiv(numeric) -> integer
659
649
  # -->
660
- # Returns the result of division `self` by `other`. The result is rounded up to
661
- # the nearest integer.
650
+ # Returns the result of division `self` by `numeric`. rounded up to the nearest
651
+ # integer.
662
652
  #
663
- # 3.ceildiv(3) # => 1
664
- # 4.ceildiv(3) # => 2
653
+ # 3.ceildiv(3) # => 1
654
+ # 4.ceildiv(3) # => 2
665
655
  #
666
- # 4.ceildiv(-3) # => -1
667
- # -4.ceildiv(3) # => -1
656
+ # 4.ceildiv(-3) # => -1
657
+ # -4.ceildiv(3) # => -1
668
658
  # -4.ceildiv(-3) # => 2
669
659
  #
670
660
  # 3.ceildiv(1.2) # => 3
@@ -712,9 +702,9 @@ class Integer < Numeric
712
702
 
713
703
  # <!--
714
704
  # rdoc-file=numeric.rb
715
- # - int.denominator -> 1
705
+ # - denominator -> 1
716
706
  # -->
717
- # Returns 1.
707
+ # Returns `1`.
718
708
  #
719
709
  def denominator: () -> Integer
720
710
 
@@ -806,9 +796,9 @@ class Integer < Numeric
806
796
 
807
797
  # <!--
808
798
  # rdoc-file=numeric.rb
809
- # - int.even? -> true or false
799
+ # - even? -> true or false
810
800
  # -->
811
- # Returns `true` if `int` is an even number.
801
+ # Returns `true` if `self` is an even number, `false` otherwise.
812
802
  #
813
803
  def even?: () -> bool
814
804
 
@@ -904,15 +894,13 @@ class Integer < Numeric
904
894
  #
905
895
  # Raises an exception if `base` is out of range.
906
896
  #
907
- # Integer#inspect is an alias for Integer#to_s.
908
- #
909
897
  alias inspect to_s
910
898
 
911
899
  # <!--
912
900
  # rdoc-file=numeric.rb
913
- # - int.integer? -> true
901
+ # - integer? -> true
914
902
  # -->
915
- # Since `int` is already an Integer, this always returns `true`.
903
+ # Since `self` is already an Integer, always returns `true`.
916
904
  #
917
905
  def integer?: () -> true
918
906
 
@@ -961,8 +949,6 @@ class Integer < Numeric
961
949
  # 10 % 3.0 # => 1.0
962
950
  # 10 % Rational(3, 1) # => (1/1)
963
951
  #
964
- # Integer#modulo is an alias for Integer#%.
965
- #
966
952
  alias modulo %
967
953
 
968
954
  def negative?: () -> bool
@@ -973,8 +959,6 @@ class Integer < Numeric
973
959
  # 1.succ #=> 2
974
960
  # -1.succ #=> 0
975
961
  #
976
- # Integer#next is an alias for Integer#succ.
977
- #
978
962
  # Related: Integer#pred (predecessor value).
979
963
  #
980
964
  def next: () -> Integer
@@ -1006,31 +990,25 @@ class Integer < Numeric
1006
990
 
1007
991
  # <!--
1008
992
  # rdoc-file=numeric.rb
1009
- # - int.numerator -> self
993
+ # - numerator -> self
1010
994
  # -->
1011
- # Returns self.
995
+ # Returns `self`.
1012
996
  #
1013
997
  def numerator: () -> Integer
1014
998
 
1015
999
  # <!--
1016
1000
  # rdoc-file=numeric.rb
1017
- # - int.odd? -> true or false
1001
+ # - odd? -> true or false
1018
1002
  # -->
1019
- # Returns `true` if `int` is an odd number.
1003
+ # Returns `true` if `self` is an odd number, `false` otherwise.
1020
1004
  #
1021
1005
  def odd?: () -> bool
1022
1006
 
1023
1007
  # <!--
1024
1008
  # rdoc-file=numeric.rb
1025
- # - int.ord -> self
1009
+ # - ord -> self
1026
1010
  # -->
1027
- # Returns the `int` itself.
1028
- #
1029
- # 97.ord #=> 97
1030
- #
1031
- # This method is intended for compatibility to character literals in Ruby 1.9.
1032
- #
1033
- # For example, `?a.ord` returns 97 both in 1.8 and 1.9.
1011
+ # Returns `self`; intended for compatibility to character literals in Ruby 1.9.
1034
1012
  #
1035
1013
  def ord: () -> Integer
1036
1014
 
@@ -1168,19 +1146,17 @@ class Integer < Numeric
1168
1146
 
1169
1147
  # <!--
1170
1148
  # rdoc-file=numeric.rb
1171
- # - int.size -> int
1149
+ # - size -> integer
1172
1150
  # -->
1173
- # Document-method: Integer#size
1151
+ # Returns the number of bytes in the machine representation of `self`; the value
1152
+ # is system-dependent:
1174
1153
  #
1175
- # Returns the number of bytes in the machine representation of `int` (machine
1176
- # dependent).
1177
- #
1178
- # 1.size #=> 8
1179
- # -1.size #=> 8
1180
- # 2147483647.size #=> 8
1181
- # (256**10 - 1).size #=> 10
1182
- # (256**20 - 1).size #=> 20
1183
- # (256**40 - 1).size #=> 40
1154
+ # 1.size # => 8
1155
+ # -1.size # => 8
1156
+ # 2147483647.size # => 8
1157
+ # (256**10 - 1).size # => 10
1158
+ # (256**20 - 1).size # => 20
1159
+ # (256**40 - 1).size # => 40
1184
1160
  #
1185
1161
  def size: () -> Integer
1186
1162
 
@@ -1204,14 +1180,12 @@ class Integer < Numeric
1204
1180
  # 1.succ #=> 2
1205
1181
  # -1.succ #=> 0
1206
1182
  #
1207
- # Integer#next is an alias for Integer#succ.
1208
- #
1209
1183
  # Related: Integer#pred (predecessor value).
1210
1184
  #
1211
1185
  def succ: () -> Integer
1212
1186
 
1213
1187
  # <!--
1214
- # rdoc-file=numeric.c
1188
+ # rdoc-file=numeric.rb
1215
1189
  # - times {|i| ... } -> self
1216
1190
  # - times -> enumerator
1217
1191
  # -->
@@ -1246,19 +1220,17 @@ class Integer < Numeric
1246
1220
 
1247
1221
  # <!--
1248
1222
  # rdoc-file=numeric.rb
1249
- # - int.to_i -> integer
1223
+ # - to_i -> self
1250
1224
  # -->
1251
- # Since `int` is already an Integer, returns `self`.
1252
- #
1253
- # #to_int is an alias for #to_i.
1225
+ # Returns `self` (which is already an Integer).
1254
1226
  #
1255
1227
  def to_i: () -> Integer
1256
1228
 
1257
1229
  # <!--
1258
1230
  # rdoc-file=numeric.rb
1259
- # - int.to_int -> integer
1231
+ # - to_int -> self
1260
1232
  # -->
1261
- # Since `int` is already an Integer, returns `self`.
1233
+ # Returns `self` (which is already an Integer).
1262
1234
  #
1263
1235
  alias to_int to_i
1264
1236
 
@@ -1290,8 +1262,6 @@ class Integer < Numeric
1290
1262
  #
1291
1263
  # Raises an exception if `base` is out of range.
1292
1264
  #
1293
- # Integer#inspect is an alias for Integer#to_s.
1294
- #
1295
1265
  def to_s: () -> String
1296
1266
  | (2) -> String
1297
1267
  | (3) -> String
@@ -1377,9 +1347,9 @@ class Integer < Numeric
1377
1347
 
1378
1348
  # <!--
1379
1349
  # rdoc-file=numeric.rb
1380
- # - int.zero? -> true or false
1350
+ # - zero? -> true or false
1381
1351
  # -->
1382
- # Returns `true` if `int` has a zero value.
1352
+ # Returns `true` if `self` has a zero value, `false` otherwise.
1383
1353
  #
1384
1354
  def zero?: () -> bool
1385
1355
 
@@ -1400,16 +1370,15 @@ class Integer < Numeric
1400
1370
 
1401
1371
  # <!--
1402
1372
  # rdoc-file=numeric.rb
1403
- # - ~int -> integer
1373
+ # - ~int -> integer
1404
1374
  # -->
1405
- # One's complement: returns a number where each bit is flipped.
1375
+ # One's complement: returns the value of `self` with each bit inverted.
1406
1376
  #
1407
- # Inverts the bits in an Integer. As integers are conceptually of infinite
1408
- # length, the result acts as if it had an infinite number of one bits to the
1409
- # left. In hex representations, this is displayed as two periods to the left of
1410
- # the digits.
1377
+ # Because an integer value is conceptually of infinite length, the result acts
1378
+ # as if it had an infinite number of one bits to the left. In hex
1379
+ # representations, this is displayed as two periods to the left of the digits:
1411
1380
  #
1412
- # sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
1381
+ # sprintf("%X", ~0x1122334455) # => "..FEEDDCCBBAA"
1413
1382
  #
1414
1383
  def ~: () -> Integer
1415
1384
  end
data/core/io/buffer.rbs CHANGED
@@ -4,8 +4,8 @@ class IO
4
4
  # IO::Buffer is a low-level efficient buffer for input/output. There are three
5
5
  # ways of using buffer:
6
6
  #
7
- # * Create an empty buffer with ::new, fill it with data using #copy or
8
- # #set_value, #set_string, get data with #get_string;
7
+ # * Create an empty buffer with ::new, fill it with buffer using #copy or
8
+ # #set_value, #set_string, get buffer with #get_string;
9
9
  # * Create a buffer mapped to some string with ::for, then it could be used
10
10
  # both for reading with #get_string or #get_value, and writing (writing will
11
11
  # change the source string, too);
@@ -39,7 +39,7 @@ class IO
39
39
  #
40
40
  # Buffer from string:
41
41
  #
42
- # string = 'data'
42
+ # string = 'buffer'
43
43
  # buffer = IO::Buffer.for(string)
44
44
  # # =>
45
45
  # # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE>
@@ -47,7 +47,7 @@ class IO
47
47
  # buffer
48
48
  # # =>
49
49
  # # #<IO::Buffer 0x00007f3f02be9b18+4 SLICE>
50
- # # 0x00000000 64 61 74 61 data
50
+ # # 0x00000000 64 61 74 61 buffer
51
51
  #
52
52
  # buffer.get_string(2) # read content starting from offset 2
53
53
  # # => "ta"
@@ -62,7 +62,7 @@ class IO
62
62
  #
63
63
  # Buffer from file:
64
64
  #
65
- # File.write('test.txt', 'test data')
65
+ # File.write('test.txt', 'test buffer')
66
66
  # # => 9
67
67
  # buffer = IO::Buffer.map(File.open('test.txt'))
68
68
  # # =>
@@ -79,7 +79,7 @@ class IO
79
79
  # buffer.set_string('---', 1)
80
80
  # # => 3 -- bytes written
81
81
  # File.read('test.txt')
82
- # # => "t--- data"
82
+ # # => "t--- buffer"
83
83
  #
84
84
  # **The class is experimental and the interface is subject to change.**
85
85
  #
@@ -94,13 +94,14 @@ class IO
94
94
  # Creates a IO::Buffer from the given string's memory. Without a block a frozen
95
95
  # internal copy of the string is created efficiently and used as the buffer
96
96
  # source. When a block is provided, the buffer is associated directly with the
97
- # string's internal data and updating the buffer will update the string.
97
+ # string's internal buffer and updating the buffer will update the string.
98
98
  #
99
99
  # Until #free is invoked on the buffer, either explicitly or via the garbage
100
100
  # collector, the source string will be locked and cannot be modified.
101
101
  #
102
102
  # If the string is frozen, it will create a read-only buffer which cannot be
103
- # modified.
103
+ # modified. If the string is shared, it may trigger a copy-on-write when using
104
+ # the block form.
104
105
  #
105
106
  # string = 'test'
106
107
  # buffer = IO::Buffer.for(string)
@@ -164,6 +165,21 @@ class IO
164
165
  #
165
166
  def self.map: (File file, ?Integer? size, ?Integer offset, ?Integer flags) -> Buffer
166
167
 
168
+ # <!--
169
+ # rdoc-file=io_buffer.c
170
+ # - IO::Buffer.string(length) {|io_buffer| ... read/write io_buffer ...} -> string
171
+ # -->
172
+ # Creates a new string of the given length and yields a IO::Buffer instance to
173
+ # the block which uses the string as a source. The block is expected to write to
174
+ # the buffer and the string will be returned.
175
+ #
176
+ # IO::Buffer.string(4) do |buffer|
177
+ # buffer.set_string("Ruby")
178
+ # end
179
+ # # => "Ruby"
180
+ #
181
+ def self.string: (int) { (Buffer) -> void } -> String
182
+
167
183
  public
168
184
 
169
185
  # <!--
@@ -212,8 +228,8 @@ class IO
212
228
  # rdoc-file=io_buffer.c
213
229
  # - copy(source, [offset, [length, [source_offset]]]) -> size
214
230
  # -->
215
- # Efficiently copy data from a source IO::Buffer into the buffer, at `offset`
216
- # using `memcpy`. For copying String instances, see #set_string.
231
+ # Efficiently copy from a source IO::Buffer into the buffer, at `offset` using
232
+ # `memcpy`. For copying String instances, see #set_string.
217
233
  #
218
234
  # buffer = IO::Buffer.new(32)
219
235
  # # =>
@@ -222,22 +238,22 @@ class IO
222
238
  # # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ *
223
239
  #
224
240
  # buffer.copy(IO::Buffer.for("test"), 8)
225
- # # => 4 -- size of data copied
241
+ # # => 4 -- size of buffer copied
226
242
  # buffer
227
243
  # # =>
228
244
  # # #<IO::Buffer 0x0000555f5cf8fe40+32 INTERNAL>
229
245
  # # 0x00000000 00 00 00 00 00 00 00 00 74 65 73 74 00 00 00 00 ........test....
230
246
  # # 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ *
231
247
  #
232
- # #copy can be used to put data into strings associated with buffer:
248
+ # #copy can be used to put buffer into strings associated with buffer:
233
249
  #
234
- # string= "data: "
235
- # # => "data: "
250
+ # string= "buffer: "
251
+ # # => "buffer: "
236
252
  # buffer = IO::Buffer.for(string)
237
253
  # buffer.copy(IO::Buffer.for("test"), 5)
238
254
  # # => 4
239
255
  # string
240
- # # => "data:test"
256
+ # # => "buffer:test"
241
257
  #
242
258
  # Attempt to copy into a read-only buffer will fail:
243
259
  #
@@ -254,12 +270,12 @@ class IO
254
270
  # File.read('test.txt')
255
271
  # # => "boom"
256
272
  #
257
- # Attempt to copy the data which will need place outside of buffer's bounds will
258
- # fail:
273
+ # Attempt to copy the buffer which will need place outside of buffer's bounds
274
+ # will fail:
259
275
  #
260
276
  # buffer = IO::Buffer.new(2)
261
277
  # buffer.copy(IO::Buffer.for('test'), 0)
262
- # # in `copy': Specified offset+length exceeds source size! (ArgumentError)
278
+ # # in `copy': Specified offset+length is bigger than the buffer size! (ArgumentError)
263
279
  #
264
280
  def copy: (Buffer source, ?Integer offset, ?Integer length, ?Integer source_offset) -> Integer
265
281
 
@@ -336,9 +352,9 @@ class IO
336
352
 
337
353
  # <!--
338
354
  # rdoc-file=io_buffer.c
339
- # - get_value(data_type, offset) -> numeric
355
+ # - get_value(buffer_type, offset) -> numeric
340
356
  # -->
341
- # Read from buffer a value of `type` at `offset`. `data_type` should be one of
357
+ # Read from buffer a value of `type` at `offset`. `buffer_type` should be one of
342
358
  # symbols:
343
359
  #
344
360
  # * `:U8`: unsigned integer, 1 byte
@@ -361,9 +377,9 @@ class IO
361
377
  # * `:F64`: double, 8 bytes, big-endian
362
378
  #
363
379
  #
364
- # A data type refers specifically to the type of binary data that is stored in
365
- # the buffer. For example, a `:u32` data type is a 32-bit unsigned integer in
366
- # little-endian format.
380
+ # A buffer type refers specifically to the type of binary buffer that is stored
381
+ # in the buffer. For example, a `:u32` buffer type is a 32-bit unsigned integer
382
+ # in little-endian format.
367
383
  #
368
384
  # Example:
369
385
  #
@@ -493,13 +509,19 @@ class IO
493
509
 
494
510
  # <!--
495
511
  # rdoc-file=io_buffer.c
496
- # - pread(io, from, length, [offset]) -> read length or -errno
512
+ # - pread(io, from, [length, [offset]]) -> read length or -errno
497
513
  # -->
498
- # Read at most `length` bytes from `io` into the buffer, starting at `from`, and
499
- # put it in buffer starting from specified `offset`. If an error occurs, return
514
+ # Read at least `length` bytes from the `io` starting at the specified `from`
515
+ # position, into the buffer starting at `offset`. If an error occurs, return
500
516
  # `-errno`.
501
517
  #
502
- # If `offset` is not given, put it at the beginning of the buffer.
518
+ # If `length` is not given or `nil`, it defaults to the size of the buffer minus
519
+ # the offset, i.e. the entire buffer.
520
+ #
521
+ # If `length` is zero, exactly one `pread` operation will occur.
522
+ #
523
+ # If `offset` is not given, it defaults to zero, i.e. the beginning of the
524
+ # buffer.
503
525
  #
504
526
  # Example:
505
527
  #
@@ -522,14 +544,24 @@ class IO
522
544
 
523
545
  # <!--
524
546
  # rdoc-file=io_buffer.c
525
- # - pwrite(io, from, length, [offset]) -> written length or -errno
547
+ # - pwrite(io, from, [length, [offset]]) -> written length or -errno
526
548
  # -->
527
- # Writes `length` bytes from buffer into `io`, starting at `offset` in the
528
- # buffer. If an error occurs, return `-errno`.
549
+ # Write at least `length` bytes from the buffer starting at `offset`, into the
550
+ # `io` starting at the specified `from` position. If an error occurs, return
551
+ # `-errno`.
552
+ #
553
+ # If `length` is not given or `nil`, it defaults to the size of the buffer minus
554
+ # the offset, i.e. the entire buffer.
529
555
  #
530
- # If `offset` is not given, the bytes are taken from the beginning of the
531
- # buffer. If the `offset` is given and is beyond the end of the file, the gap
532
- # will be filled with null (0 value) bytes.
556
+ # If `length` is zero, exactly one `pwrite` operation will occur.
557
+ #
558
+ # If `offset` is not given, it defaults to zero, i.e. the beginning of the
559
+ # buffer.
560
+ #
561
+ # If the `from` position is beyond the end of the file, the gap will be filled
562
+ # with null (0 value) bytes.
563
+ #
564
+ # Example:
533
565
  #
534
566
  # out = File.open('output.txt', File::RDWR) # open for read/write, no truncation
535
567
  # IO::Buffer.for('1234567').pwrite(out, 2, 3, 1)
@@ -541,14 +573,18 @@ class IO
541
573
 
542
574
  # <!--
543
575
  # rdoc-file=io_buffer.c
544
- # - read(io, length, [offset]) -> read length or -errno
576
+ # - read(io, [length, [offset]]) -> read length or -errno
545
577
  # -->
546
- # Read at most `length` bytes from `io` into the buffer, starting at `offset`.
547
- # If an error occurs, return `-errno`.
578
+ # Read at least `length` bytes from the `io`, into the buffer starting at
579
+ # `offset`. If an error occurs, return `-errno`.
548
580
  #
549
- # If `offset` is not given, read from the beginning of the buffer.
581
+ # If `length` is not given or `nil`, it defaults to the size of the buffer minus
582
+ # the offset, i.e. the entire buffer.
550
583
  #
551
- # If `length` is 0, read nothing.
584
+ # If `length` is zero, exactly one `read` operation will occur.
585
+ #
586
+ # If `offset` is not given, it defaults to zero, i.e. the beginning of the
587
+ # buffer.
552
588
  #
553
589
  # Example:
554
590
  #
@@ -600,15 +636,15 @@ class IO
600
636
  # rdoc-file=io_buffer.c
601
637
  # - set_string(string, [offset, [length, [source_offset]]]) -> size
602
638
  # -->
603
- # Efficiently copy data from a source String into the buffer, at `offset` using
604
- # `memcpy`.
639
+ # Efficiently copy buffer from a source String into the buffer, at `offset`
640
+ # using `memcpy`.
605
641
  #
606
642
  # buf = IO::Buffer.new(8)
607
643
  # # =>
608
644
  # # #<IO::Buffer 0x0000557412714a20+8 INTERNAL>
609
645
  # # 0x00000000 00 00 00 00 00 00 00 00 ........
610
646
  #
611
- # # set data starting from offset 1, take 2 bytes starting from string's
647
+ # # set buffer starting from offset 1, take 2 bytes starting from string's
612
648
  # # second
613
649
  # buf.set_string('test', 1, 2, 1)
614
650
  # # => 2
@@ -667,7 +703,7 @@ class IO
667
703
 
668
704
  # <!--
669
705
  # rdoc-file=io_buffer.c
670
- # - slice([offset = 0, [length]]) -> io_buffer
706
+ # - slice([offset, [length]]) -> io_buffer
671
707
  # -->
672
708
  # Produce another IO::Buffer which is a slice (or view into) the current one
673
709
  # starting at `offset` bytes and going for `length` bytes.
@@ -762,7 +798,7 @@ class IO
762
798
  # rdoc-file=io_buffer.c
763
799
  # - valid? -> true or false
764
800
  # -->
765
- # Returns whether the buffer data is accessible.
801
+ # Returns whether the buffer buffer is accessible.
766
802
  #
767
803
  # A buffer becomes invalid if it is a slice of another buffer which has been
768
804
  # freed.
@@ -771,14 +807,21 @@ class IO
771
807
 
772
808
  # <!--
773
809
  # rdoc-file=io_buffer.c
774
- # - write(io, length, [offset]) -> written length or -errno
810
+ # - write(io, [length, [offset]]) -> written length or -errno
775
811
  # -->
776
- # Writes `length` bytes from buffer into `io`, starting at `offset` in the
777
- # buffer. If an error occurs, return `-errno`.
812
+ # Write at least `length` bytes from the buffer starting at `offset`, into the
813
+ # `io`. If an error occurs, return `-errno`.
814
+ #
815
+ # If `length` is not given or `nil`, it defaults to the size of the buffer minus
816
+ # the offset, i.e. the entire buffer.
817
+ #
818
+ # If `length` is zero, exactly one `write` operation will occur.
778
819
  #
779
- # If `offset` is not given, the bytes are taken from the beginning of the
820
+ # If `offset` is not given, it defaults to zero, i.e. the beginning of the
780
821
  # buffer.
781
822
  #
823
+ # Example:
824
+ #
782
825
  # out = File.open('output.txt', 'wb')
783
826
  # IO::Buffer.for('1234567').write(out, 3)
784
827
  #