rbs 3.8.0.pre.1 → 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/core/exception.rbs CHANGED
@@ -19,7 +19,7 @@
19
19
  #
20
20
  # * NoMemoryError
21
21
  # * ScriptError
22
- # * [LoadError](https://docs.ruby-lang.org/en/master/LoadError.html)
22
+ # * LoadError
23
23
  # * NotImplementedError
24
24
  # * SyntaxError
25
25
  # * SecurityError
@@ -51,7 +51,7 @@
51
51
  # * ZeroDivisionError
52
52
  # * SystemExit
53
53
  # * SystemStackError
54
- # * [fatal](https://docs.ruby-lang.org/en/master/fatal.html)
54
+ # * [fatal](rdoc-ref:fatal)
55
55
  #
56
56
  class Exception
57
57
  # <!--
@@ -99,24 +99,31 @@ class Exception
99
99
  # rdoc-file=error.c
100
100
  # - backtrace -> array or nil
101
101
  # -->
102
- # Returns a backtrace value for `self`; the returned value depends on the form
103
- # of the stored backtrace value:
102
+ # Returns the backtrace (the list of code locations that led to the exception),
103
+ # as an array of strings.
104
104
  #
105
- # * Array of Thread::Backtrace::Location objects: returns the array of strings
106
- # given by `Exception#backtrace_locations.map {|loc| loc.to_s }`. This is
107
- # the normal case, where the backtrace value was stored by Kernel#raise.
108
- # * Array of strings: returns that array. This is the unusual case, where the
109
- # backtrace value was explicitly stored as an array of strings.
110
- # * `nil`: returns `nil`.
105
+ # Example (assuming the code is stored in the file named `t.rb`):
111
106
  #
112
- # Example:
107
+ # def division(numerator, denominator)
108
+ # numerator / denominator
109
+ # end
113
110
  #
114
111
  # begin
115
- # 1 / 0
116
- # rescue => x
117
- # x.backtrace.take(2)
112
+ # division(1, 0)
113
+ # rescue => ex
114
+ # p ex.backtrace
115
+ # # ["t.rb:2:in 'Integer#/'", "t.rb:2:in 'Object#division'", "t.rb:6:in '<main>'"]
116
+ # loc = ex.backtrace.first
117
+ # p loc.class
118
+ # # String
118
119
  # end
119
- # # => ["(irb):132:in `/'", "(irb):132:in `<top (required)>'"]
120
+ #
121
+ # The value returned by this method migth be adjusted when raising (see
122
+ # Kernel#raise), or during intermediate handling by #set_backtrace.
123
+ #
124
+ # See also #backtrace_locations that provide the same value, as structured
125
+ # objects. (Note though that two values might not be consistent with each other
126
+ # when backtraces are manually adjusted.)
120
127
  #
121
128
  # see [Backtraces](rdoc-ref:exceptions.md@Backtraces).
122
129
  #
@@ -126,20 +133,37 @@ class Exception
126
133
  # rdoc-file=error.c
127
134
  # - backtrace_locations -> array or nil
128
135
  # -->
129
- # Returns a backtrace value for `self`; the returned value depends on the form
130
- # of the stored backtrace value:
136
+ # Returns the backtrace (the list of code locations that led to the exception),
137
+ # as an array of Thread::Backtrace::Location instances.
131
138
  #
132
- # * Array of Thread::Backtrace::Location objects: returns that array.
133
- # * Array of strings or `nil`: returns `nil`.
139
+ # Example (assuming the code is stored in the file named `t.rb`):
134
140
  #
135
- # Example:
141
+ # def division(numerator, denominator)
142
+ # numerator / denominator
143
+ # end
136
144
  #
137
145
  # begin
138
- # 1 / 0
139
- # rescue => x
140
- # x.backtrace_locations.take(2)
146
+ # division(1, 0)
147
+ # rescue => ex
148
+ # p ex.backtrace_locations
149
+ # # ["t.rb:2:in 'Integer#/'", "t.rb:2:in 'Object#division'", "t.rb:6:in '<main>'"]
150
+ # loc = ex.backtrace_locations.first
151
+ # p loc.class
152
+ # # Thread::Backtrace::Location
153
+ # p loc.path
154
+ # # "t.rb"
155
+ # p loc.lineno
156
+ # # 2
157
+ # p loc.label
158
+ # # "Integer#/"
141
159
  # end
142
- # # => ["(irb):150:in `/'", "(irb):150:in `<top (required)>'"]
160
+ #
161
+ # The value returned by this method might be adjusted when raising (see
162
+ # Kernel#raise), or during intermediate handling by #set_backtrace.
163
+ #
164
+ # See also #backtrace that provide the same value as an array of strings. (Note
165
+ # though that two values might not be consistent with each other when backtraces
166
+ # are manually adjusted.)
143
167
  #
144
168
  # See [Backtraces](rdoc-ref:exceptions.md@Backtraces).
145
169
  #
@@ -294,15 +318,100 @@ class Exception
294
318
  # rdoc-file=error.c
295
319
  # - set_backtrace(value) -> value
296
320
  # -->
297
- # Sets the backtrace value for `self`; returns the given +value:
321
+ # Sets the backtrace value for `self`; returns the given `value`.
298
322
  #
299
- # x = RuntimeError.new('Boom')
300
- # x.set_backtrace(%w[foo bar baz]) # => ["foo", "bar", "baz"]
301
- # x.backtrace # => ["foo", "bar", "baz"]
323
+ # The `value` might be:
302
324
  #
303
- # The given `value` must be an array of strings, a single string, or `nil`.
325
+ # * an array of Thread::Backtrace::Location;
326
+ # * an array of String instances;
327
+ # * a single String instance; or
328
+ # * `nil`.
329
+ #
330
+ # Using array of Thread::Backtrace::Location is the most consistent option: it
331
+ # sets both #backtrace and #backtrace_locations. It should be preferred when
332
+ # possible. The suitable array of locations can be obtained from
333
+ # Kernel#caller_locations, copied from another error, or just set to the
334
+ # adjusted result of the current error's #backtrace_locations:
335
+ #
336
+ # require 'json'
337
+ #
338
+ # def parse_payload(text)
339
+ # JSON.parse(text) # test.rb, line 4
340
+ # rescue JSON::ParserError => ex
341
+ # ex.set_backtrace(ex.backtrace_locations[2...])
342
+ # raise
343
+ # end
304
344
  #
305
- # Does not affect the value returned by #backtrace_locations.
345
+ # parse_payload('{"wrong: "json"')
346
+ # # test.rb:4:in 'Object#parse_payload': unexpected token at '{"wrong: "json"' (JSON::ParserError)
347
+ # #
348
+ # # An error points to the body of parse_payload method,
349
+ # # hiding the parts of the backtrace related to the internals
350
+ # # of the "json" library
351
+ #
352
+ # # The error has both #backtace and #backtrace_locations set
353
+ # # consistently:
354
+ # begin
355
+ # parse_payload('{"wrong: "json"')
356
+ # rescue => ex
357
+ # p ex.backtrace
358
+ # # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"]
359
+ # p ex.backtrace_locations
360
+ # # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"]
361
+ # end
362
+ #
363
+ # When the desired stack of locations is not available and should be constructed
364
+ # from scratch, an array of strings or a singular string can be used. In this
365
+ # case, only #backtrace is affected:
366
+ #
367
+ # def parse_payload(text)
368
+ # JSON.parse(text)
369
+ # rescue JSON::ParserError => ex
370
+ # ex.set_backtrace(["dsl.rb:34", "framework.rb:1"])
371
+ # # The error have the new value in #backtrace:
372
+ # p ex.backtrace
373
+ # # ["dsl.rb:34", "framework.rb:1"]
374
+ #
375
+ # # but the original one in #backtrace_locations
376
+ # p ex.backtrace_locations
377
+ # # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...]
378
+ # end
379
+ #
380
+ # parse_payload('{"wrong: "json"')
381
+ #
382
+ # Calling #set_backtrace with `nil` clears up #backtrace but doesn't affect
383
+ # #backtrace_locations:
384
+ #
385
+ # def parse_payload(text)
386
+ # JSON.parse(text)
387
+ # rescue JSON::ParserError => ex
388
+ # ex.set_backtrace(nil)
389
+ # p ex.backtrace
390
+ # # nil
391
+ # p ex.backtrace_locations
392
+ # # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...]
393
+ # end
394
+ #
395
+ # parse_payload('{"wrong: "json"')
396
+ #
397
+ # On reraising of such an exception, both #backtrace and #backtrace_locations is
398
+ # set to the place of reraising:
399
+ #
400
+ # def parse_payload(text)
401
+ # JSON.parse(text)
402
+ # rescue JSON::ParserError => ex
403
+ # ex.set_backtrace(nil)
404
+ # raise # test.rb, line 7
405
+ # end
406
+ #
407
+ # begin
408
+ # parse_payload('{"wrong: "json"')
409
+ # rescue => ex
410
+ # p ex.backtrace
411
+ # # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"]
412
+ # p ex.backtrace_locations
413
+ # # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"]
414
+ # end
306
415
  #
307
416
  # See [Backtraces](rdoc-ref:exceptions.md@Backtraces).
308
417
  #
@@ -358,16 +467,16 @@ class Exception
358
467
  # Output:
359
468
  #
360
469
  # "divided by 0"
361
- # ["t.rb:3:in `/': divided by 0 (ZeroDivisionError)",
362
- # "\tfrom t.rb:3:in `baz'",
363
- # "\tfrom t.rb:10:in `bar'",
364
- # "\tfrom t.rb:11:in `foo'",
365
- # "\tfrom t.rb:12:in `<main>'"]
366
- # ["t.rb:3:in `/': \e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m",
367
- # "\tfrom t.rb:3:in `baz'",
368
- # "\tfrom t.rb:10:in `bar'",
369
- # "\tfrom t.rb:11:in `foo'",
370
- # "\tfrom t.rb:12:in `<main>'"]
470
+ # ["t.rb:3:in 'Integer#/': divided by 0 (ZeroDivisionError)",
471
+ # "\tfrom t.rb:3:in 'Object#baz'",
472
+ # "\tfrom t.rb:10:in 'Object#bar'",
473
+ # "\tfrom t.rb:11:in 'Object#foo'",
474
+ # "\tfrom t.rb:12:in '<main>'"]
475
+ # ["t.rb:3:in 'Integer#/': \e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m",
476
+ # "\tfrom t.rb:3:in 'Object#baz'",
477
+ # "\tfrom t.rb:10:in 'Object#bar'",
478
+ # "\tfrom t.rb:11:in 'Object#foo'",
479
+ # "\tfrom t.rb:12:in '<main>'"]
371
480
  #
372
481
  # An overriding method should be careful with ANSI code enhancements; see
373
482
  # [Messages](rdoc-ref:exceptions.md@Messages).
data/core/file.rbs CHANGED
@@ -1314,7 +1314,7 @@ class File < IO
1314
1314
  #
1315
1315
  # File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
1316
1316
  #
1317
- def self.join: (*string) -> String
1317
+ def self.join: (*path) -> String
1318
1318
 
1319
1319
  # <!--
1320
1320
  # rdoc-file=file.c
@@ -2295,43 +2295,36 @@ File::Constants::DSYNC: Integer
2295
2295
  File::Constants::EXCL: Integer
2296
2296
 
2297
2297
  # <!-- rdoc-file=dir.c -->
2298
- # FNM_CASEFOLD
2299
2298
  # [File::FNM_CASEFOLD](rdoc-ref:File::Constants@File-3A-3AFNM_CASEFOLD)
2300
2299
  #
2301
2300
  File::Constants::FNM_CASEFOLD: Integer
2302
2301
 
2303
2302
  # <!-- rdoc-file=dir.c -->
2304
- # FNM_DOTMATCH
2305
2303
  # [File::FNM_DOTMATCH](rdoc-ref:File::Constants@File-3A-3AFNM_DOTMATCH)
2306
2304
  #
2307
2305
  File::Constants::FNM_DOTMATCH: Integer
2308
2306
 
2309
2307
  # <!-- rdoc-file=dir.c -->
2310
- # FNM_EXTGLOB
2311
2308
  # [File::FNM_EXTGLOB](rdoc-ref:File::Constants@File-3A-3AFNM_EXTGLOB)
2312
2309
  #
2313
2310
  File::Constants::FNM_EXTGLOB: Integer
2314
2311
 
2315
2312
  # <!-- rdoc-file=dir.c -->
2316
- # FNM_NOESCAPE
2317
2313
  # [File::FNM_NOESCAPE](rdoc-ref:File::Constants@File-3A-3AFNM_NOESCAPE)
2318
2314
  #
2319
2315
  File::Constants::FNM_NOESCAPE: Integer
2320
2316
 
2321
2317
  # <!-- rdoc-file=dir.c -->
2322
- # FNM_PATHNAME
2323
2318
  # [File::FNM_PATHNAME](rdoc-ref:File::Constants@File-3A-3AFNM_PATHNAME)
2324
2319
  #
2325
2320
  File::Constants::FNM_PATHNAME: Integer
2326
2321
 
2327
2322
  # <!-- rdoc-file=dir.c -->
2328
- # FNM_SHORTNAME
2329
2323
  # [File::FNM_SHORTNAME](rdoc-ref:File::Constants@File-3A-3AFNM_SHORTNAME)
2330
2324
  #
2331
2325
  File::Constants::FNM_SHORTNAME: Integer
2332
2326
 
2333
2327
  # <!-- rdoc-file=dir.c -->
2334
- # FNM_SYSCASE
2335
2328
  # [File::FNM_SYSCASE](rdoc-ref:File::Constants@File-3A-3AFNM_SYSCASE)
2336
2329
  #
2337
2330
  File::Constants::FNM_SYSCASE: Integer
data/core/gc.rbs CHANGED
@@ -167,7 +167,8 @@ module GC
167
167
  # Configuration parameters are GC implementation-specific and may change without
168
168
  # notice.
169
169
  #
170
- # This method can be called without parameters to retrieve the current config.
170
+ # This method can be called without parameters to retrieve the current config as
171
+ # a `Hash` with `Symbol` keys.
171
172
  #
172
173
  # This method can also be called with a `Hash` argument to assign values to
173
174
  # valid config keys. Config keys missing from the passed `Hash` will be left
@@ -185,7 +186,24 @@ module GC
185
186
  #
186
187
  # This method is only expected to work on CRuby.
187
188
  #
188
- # Valid config keys for Ruby's default GC implementation are:
189
+ # ### GC Implementation independent values
190
+ #
191
+ # The `GC.config` hash can also contain keys that are global and read-only.
192
+ # These keys are not specific to any one GC library implementation and
193
+ # attempting to write to them will raise `ArgumentError`.
194
+ #
195
+ # There is currently only one global, read-only key:
196
+ #
197
+ # implementation
198
+ # : Returns a `String` containing the name of the currently loaded GC library,
199
+ # if one has been loaded using `RUBY_GC_LIBRARY`, and "default" in all other
200
+ # cases
201
+ #
202
+ #
203
+ # ### GC Implementation specific values
204
+ #
205
+ # GC libraries are expected to document their own configuration. Valid keys for
206
+ # Ruby's default GC implementation are:
189
207
  #
190
208
  # rgengc_allow_full_mark
191
209
  # : Controls whether the GC is allowed to run a full mark (young & old
@@ -569,7 +587,7 @@ module GC
569
587
 
570
588
  # The type that `GC.compact` and related functions can return.
571
589
  #
572
- type compact_info = Hash[:considered | :moved |:moved_up | :moved_down, Hash[Symbol, Integer]]
590
+ type compact_info = Hash[:considered | :moved | :moved_up | :moved_down, Hash[Symbol, Integer]]
573
591
 
574
592
  # <!--
575
593
  # rdoc-file=gc.rb
data/core/hash.rbs CHANGED
@@ -1226,7 +1226,7 @@ class Hash[unchecked out K, unchecked out V] < Object
1226
1226
  # Returns a new String containing the hash entries:
1227
1227
  #
1228
1228
  # h = {foo: 0, bar: 1, baz: 2}
1229
- # h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
1229
+ # h.inspect # => "{foo: 0, bar: 1, baz: 2}"
1230
1230
  #
1231
1231
  def inspect: () -> String
1232
1232
 
@@ -1628,7 +1628,7 @@ class Hash[unchecked out K, unchecked out V] < Object
1628
1628
  # Returns a new String containing the hash entries:
1629
1629
  #
1630
1630
  # h = {foo: 0, bar: 1, baz: 2}
1631
- # h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
1631
+ # h.inspect # => "{foo: 0, bar: 1, baz: 2}"
1632
1632
  #
1633
1633
  alias to_s inspect
1634
1634
 
data/core/io.rbs CHANGED
@@ -3104,8 +3104,8 @@ class IO < Object
3104
3104
  #
3105
3105
  # Returns an Enumerator if no block is given.
3106
3106
  #
3107
- def each_line: (?String sep, ?Integer limit) { (String line) -> void } -> self
3108
- | (?String sep, ?Integer limit) -> ::Enumerator[String, self]
3107
+ def each_line: (?string sep, ?int limit, ?chomp: boolish) { (String line) -> void } -> self
3108
+ | (?string sep, ?int limit, ?chomp: boolish) -> ::Enumerator[String, self]
3109
3109
 
3110
3110
  # <!--
3111
3111
  # rdoc-file=io.c
data/core/kernel.rbs CHANGED
@@ -675,7 +675,7 @@ module Kernel : BasicObject
675
675
  #
676
676
  # String([0, 1, 2]) # => "[0, 1, 2]"
677
677
  # String(0..5) # => "0..5"
678
- # String({foo: 0, bar: 1}) # => "{:foo=>0, :bar=>1}"
678
+ # String({foo: 0, bar: 1}) # => "{foo: 0, bar: 1}"
679
679
  #
680
680
  # Raises `TypeError` if `object` cannot be converted to a string.
681
681
  #
@@ -936,16 +936,37 @@ module Kernel : BasicObject
936
936
  #
937
937
  # See [Messages](rdoc-ref:exceptions.md@Messages).
938
938
  #
939
- # Argument `backtrace` sets the stored backtrace in the new exception, which may
940
- # be retrieved by method Exception#backtrace; the backtrace must be an array of
941
- # strings or `nil`:
939
+ # Argument `backtrace` might be used to modify the backtrace of the new
940
+ # exception, as reported by Exception#backtrace and
941
+ # Exception#backtrace_locations; the backtrace must be an array of
942
+ # Thread::Backtrace::Location, an array of strings, a single string, or `nil`.
943
+ #
944
+ # Using the array of Thread::Backtrace::Location instances is the most
945
+ # consistent option and should be preferred when possible. The necessary value
946
+ # might be obtained from #caller_locations, or copied from
947
+ # Exception#backtrace_locations of another error:
942
948
  #
943
949
  # begin
944
- # raise(StandardError, 'Boom', %w[foo bar baz])
945
- # rescue => x
946
- # p x.backtrace
950
+ # do_some_work()
951
+ # rescue ZeroDivisionError => ex
952
+ # raise(LogicalError, "You have an error in your math", ex.backtrace_locations)
953
+ # end
954
+ #
955
+ # The ways, both Exception#backtrace and Exception#backtrace_locations of the
956
+ # raised error are set to the same backtrace.
957
+ #
958
+ # When the desired stack of locations is not available and should be constructed
959
+ # from scratch, an array of strings or a singular string can be used. In this
960
+ # case, only Exception#backtrace is set:
961
+ #
962
+ # begin
963
+ # raise(StandardError, 'Boom', %w[dsl.rb:3 framework.rb:1])
964
+ # rescue => ex
965
+ # p ex.backtrace
966
+ # # => ["dsl.rb:3", "framework.rb:1"]
967
+ # p ex.backtrace_locations
968
+ # # => nil
947
969
  # end
948
- # # => ["foo", "bar", "baz"]
949
970
  #
950
971
  # If argument `backtrace` is not given, the backtrace is set according to an
951
972
  # array of Thread::Backtrace::Location objects, as derived from the call stack.
@@ -1021,16 +1042,37 @@ module Kernel : BasicObject
1021
1042
  #
1022
1043
  # See [Messages](rdoc-ref:exceptions.md@Messages).
1023
1044
  #
1024
- # Argument `backtrace` sets the stored backtrace in the new exception, which may
1025
- # be retrieved by method Exception#backtrace; the backtrace must be an array of
1026
- # strings or `nil`:
1045
+ # Argument `backtrace` might be used to modify the backtrace of the new
1046
+ # exception, as reported by Exception#backtrace and
1047
+ # Exception#backtrace_locations; the backtrace must be an array of
1048
+ # Thread::Backtrace::Location, an array of strings, a single string, or `nil`.
1049
+ #
1050
+ # Using the array of Thread::Backtrace::Location instances is the most
1051
+ # consistent option and should be preferred when possible. The necessary value
1052
+ # might be obtained from #caller_locations, or copied from
1053
+ # Exception#backtrace_locations of another error:
1027
1054
  #
1028
1055
  # begin
1029
- # raise(StandardError, 'Boom', %w[foo bar baz])
1030
- # rescue => x
1031
- # p x.backtrace
1056
+ # do_some_work()
1057
+ # rescue ZeroDivisionError => ex
1058
+ # raise(LogicalError, "You have an error in your math", ex.backtrace_locations)
1059
+ # end
1060
+ #
1061
+ # The ways, both Exception#backtrace and Exception#backtrace_locations of the
1062
+ # raised error are set to the same backtrace.
1063
+ #
1064
+ # When the desired stack of locations is not available and should be constructed
1065
+ # from scratch, an array of strings or a singular string can be used. In this
1066
+ # case, only Exception#backtrace is set:
1067
+ #
1068
+ # begin
1069
+ # raise(StandardError, 'Boom', %w[dsl.rb:3 framework.rb:1])
1070
+ # rescue => ex
1071
+ # p ex.backtrace
1072
+ # # => ["dsl.rb:3", "framework.rb:1"]
1073
+ # p ex.backtrace_locations
1074
+ # # => nil
1032
1075
  # end
1033
- # # => ["foo", "bar", "baz"]
1034
1076
  #
1035
1077
  # If argument `backtrace` is not given, the backtrace is set according to an
1036
1078
  # array of Thread::Backtrace::Location objects, as derived from the call stack.
@@ -1499,7 +1541,7 @@ module Kernel : BasicObject
1499
1541
  # Optional keyword arguments `enc_opts` specify encoding options; see [Encoding
1500
1542
  # options](rdoc-ref:encodings.rdoc@Encoding+Options).
1501
1543
  #
1502
- def self?.readlines: (?String arg0, ?Integer arg1) -> ::Array[String]
1544
+ def self?.readlines: (?string sep, ?int limit, ?chomp: boolish) -> ::Array[String]
1503
1545
 
1504
1546
  # <!--
1505
1547
  # rdoc-file=lib/rubygems/core_ext/kernel_require.rb
data/core/nil_class.rbs CHANGED
@@ -18,6 +18,9 @@
18
18
  # * #to_r
19
19
  # * #to_s
20
20
  #
21
+ # While `nil` doesn't have an explicitly defined #to_hash method, it can be used
22
+ # in `**` unpacking, not adding any keyword arguments.
23
+ #
21
24
  # Another method provides inspection:
22
25
  #
23
26
  # * #inspect
data/core/numeric.rbs CHANGED
@@ -776,7 +776,7 @@ class Numeric
776
776
  # Rational(1, 2).to_int # => 0
777
777
  # Rational(2, 1).to_int # => 2
778
778
  # Complex(2, 0).to_int # => 2
779
- # Complex(2, 1) # Raises RangeError (non-zero imaginary part)
779
+ # Complex(2, 1).to_int # Raises RangeError (non-zero imaginary part)
780
780
  #
781
781
  def to_int: () -> Integer
782
782
 
data/core/proc.rbs CHANGED
@@ -236,19 +236,86 @@
236
236
  # Since `return` and `break` exits the block itself in lambdas, lambdas cannot
237
237
  # be orphaned.
238
238
  #
239
- # ## Numbered parameters
239
+ # ## Anonymous block parameters
240
240
  #
241
- # Numbered parameters are implicitly defined block parameters intended to
242
- # simplify writing short blocks:
241
+ # To simplify writing short blocks, Ruby provides two different types of
242
+ # anonymous parameters: `it` (single parameter) and numbered ones: `_1`, `_2`
243
+ # and so on.
243
244
  #
244
245
  # # Explicit parameter:
245
246
  # %w[test me please].each { |str| puts str.upcase } # prints TEST, ME, PLEASE
246
247
  # (1..5).map { |i| i**2 } # => [1, 4, 9, 16, 25]
247
248
  #
248
- # # Implicit parameter:
249
+ # # it:
250
+ # %w[test me please].each { puts it.upcase } # prints TEST, ME, PLEASE
251
+ # (1..5).map { it**2 } # => [1, 4, 9, 16, 25]
252
+ #
253
+ # # Numbered parameter:
249
254
  # %w[test me please].each { puts _1.upcase } # prints TEST, ME, PLEASE
250
255
  # (1..5).map { _1**2 } # => [1, 4, 9, 16, 25]
251
256
  #
257
+ # ### `it`
258
+ #
259
+ # `it` is a name that is available inside a block when no explicit parameters
260
+ # defined, as shown above.
261
+ #
262
+ # %w[test me please].each { puts it.upcase } # prints TEST, ME, PLEASE
263
+ # (1..5).map { it**2 } # => [1, 4, 9, 16, 25]
264
+ #
265
+ # `it` is a "soft keyword": it is not a reserved name, and can be used as a name
266
+ # for methods and local variables:
267
+ #
268
+ # it = 5 # no warnings
269
+ # def it(&block) # RSpec-like API, no warnings
270
+ # # ...
271
+ # end
272
+ #
273
+ # `it` can be used as a local variable even in blocks that use it as an implicit
274
+ # parameter (though this style is obviously confusing):
275
+ #
276
+ # [1, 2, 3].each {
277
+ # # takes a value of implicit parameter "it" and uses it to
278
+ # # define a local variable with the same name
279
+ # it = it**2
280
+ # p it
281
+ # }
282
+ #
283
+ # In a block with explicit parameters defined `it` usage raises an exception:
284
+ #
285
+ # [1, 2, 3].each { |x| p it }
286
+ # # syntax error found (SyntaxError)
287
+ # # [1, 2, 3].each { |x| p it }
288
+ # # ^~ `it` is not allowed when an ordinary parameter is defined
289
+ #
290
+ # But if a local name (variable or method) is available, it would be used:
291
+ #
292
+ # it = 5
293
+ # [1, 2, 3].each { |x| p it }
294
+ # # Prints 5, 5, 5
295
+ #
296
+ # Blocks using `it` can be nested:
297
+ #
298
+ # %w[test me].each { it.each_char { p it } }
299
+ # # Prints "t", "e", "s", "t", "m", "e"
300
+ #
301
+ # Blocks using `it` are considered to have one parameter:
302
+ #
303
+ # p = proc { it**2 }
304
+ # l = lambda { it**2 }
305
+ # p.parameters # => [[:opt, nil]]
306
+ # p.arity # => 1
307
+ # l.parameters # => [[:req]]
308
+ # l.arity # => 1
309
+ #
310
+ # ### Numbered parameters
311
+ #
312
+ # Numbered parameters are another way to name block parameters implicitly.
313
+ # Unlike `it`, numbered parameters allow to refer to several parameters in one
314
+ # block.
315
+ #
316
+ # %w[test me please].each { puts _1.upcase } # prints TEST, ME, PLEASE
317
+ # {a: 100, b: 200}.map { "#{_1} = #{_2}" } # => "a = 100", "b = 200"
318
+ #
252
319
  # Parameter names from `_1` to `_9` are supported:
253
320
  #
254
321
  # [10, 20, 30].zip([40, 50, 60], [70, 80, 90]).map { _1 + _2 + _3 }
@@ -262,11 +329,16 @@
262
329
  # [10, 20, 30].map { |x| _1**2 }
263
330
  # # SyntaxError (ordinary parameter is defined)
264
331
  #
332
+ # Numbered parameters can't be mixed with `it` either:
333
+ #
334
+ # [10, 20, 30].map { _1 + it }
335
+ # # SyntaxError: `it` is not allowed when a numbered parameter is already used
336
+ #
265
337
  # To avoid conflicts, naming local variables or method arguments `_1`, `_2` and
266
- # so on, causes a warning.
338
+ # so on, causes an error.
267
339
  #
268
- # _1 = 'test'
269
- # # warning: `_1' is reserved as numbered parameter
340
+ # _1 = 'test'
341
+ # # ^~ _1 is reserved for numbered parameters (SyntaxError)
270
342
  #
271
343
  # Using implicit numbered parameters affects block's arity:
272
344
  #
@@ -280,12 +352,10 @@
280
352
  # Blocks with numbered parameters can't be nested:
281
353
  #
282
354
  # %w[test me].each { _1.each_char { p _1 } }
283
- # # SyntaxError (numbered parameter is already used in outer block here)
355
+ # # numbered parameter is already used in outer block (SyntaxError)
284
356
  # # %w[test me].each { _1.each_char { p _1 } }
285
357
  # # ^~
286
358
  #
287
- # Numbered parameters were introduced in Ruby 2.7.
288
- #
289
359
  class Proc
290
360
  interface _Callable
291
361
  def call: (?) -> untyped
data/core/ractor.rbs CHANGED
@@ -588,6 +588,23 @@ class Ractor
588
588
  #
589
589
  def self.shareable?: (untyped obj) -> bool
590
590
 
591
+ # <!--
592
+ # rdoc-file=ractor.rb
593
+ # - Ractor.store_if_absent(key){ init_block }
594
+ # -->
595
+ # If the correponding value is not set, yield a value with init_block and store
596
+ # the value in thread-safe manner. This method returns corresponding stored
597
+ # value.
598
+ #
599
+ # (1..10).map{
600
+ # Thread.new(it){|i|
601
+ # Ractor.store_if_absent(:s){ f(); i }
602
+ # #=> return stored value of key :s
603
+ # }
604
+ # }.map(&:value).uniq.size #=> 1 and f() is called only once
605
+ #
606
+ def self.store_if_absent: [A] (Symbol) { (nil) -> A } -> A
607
+
591
608
  # <!--
592
609
  # rdoc-file=ractor.rb
593
610
  # - Ractor.yield(msg, move: false) -> nil
@@ -1036,6 +1053,9 @@ class Ractor
1036
1053
  # end
1037
1054
  #
1038
1055
  class RemoteError < Ractor::Error
1056
+ # <!-- rdoc-file=ractor.rb -->
1057
+ # The Ractor an uncaught exception is raised in.
1058
+ #
1039
1059
  def ractor: () -> Ractor
1040
1060
  end
1041
1061