rbs 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -0
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +6 -1
  5. data/core/array.rbs +2866 -1086
  6. data/core/basic_object.rbs +150 -30
  7. data/core/binding.rbs +33 -0
  8. data/core/builtin.rbs +4 -4
  9. data/core/class.rbs +43 -5
  10. data/core/comparable.rbs +57 -0
  11. data/core/complex.rbs +170 -4
  12. data/core/constants.rbs +51 -0
  13. data/core/deprecated.rbs +7 -0
  14. data/core/dir.rbs +305 -20
  15. data/core/encoding.rbs +472 -77
  16. data/core/enumerable.rbs +2173 -234
  17. data/core/enumerator.rbs +448 -182
  18. data/core/env.rbs +448 -1
  19. data/core/errno.rbs +1 -10
  20. data/core/errors.rbs +152 -2
  21. data/core/exception.rbs +201 -127
  22. data/core/false_class.rbs +27 -0
  23. data/core/fiber.rbs +118 -37
  24. data/core/fiber_error.rbs +8 -9
  25. data/core/file.rbs +1059 -139
  26. data/core/file_test.rbs +287 -32
  27. data/core/float.rbs +776 -300
  28. data/core/gc.rbs +185 -34
  29. data/core/global_variables.rbs +5 -1
  30. data/core/hash.rbs +1582 -649
  31. data/core/integer.rbs +974 -204
  32. data/core/io/buffer.rbs +710 -0
  33. data/core/io/wait.rbs +29 -8
  34. data/core/io.rbs +2438 -417
  35. data/core/kernel.rbs +2315 -316
  36. data/core/marshal.rbs +37 -2
  37. data/core/match_data.rbs +123 -6
  38. data/core/math.rbs +126 -6
  39. data/core/method.rbs +226 -102
  40. data/core/module.rbs +421 -45
  41. data/core/nil_class.rbs +64 -0
  42. data/core/numeric.rbs +620 -142
  43. data/core/object.rbs +453 -81
  44. data/core/object_space.rbs +92 -2
  45. data/core/proc.rbs +482 -285
  46. data/core/process.rbs +443 -34
  47. data/core/ractor.rbs +232 -9
  48. data/core/random.rbs +151 -52
  49. data/core/range.rbs +885 -160
  50. data/core/rational.rbs +122 -6
  51. data/core/rb_config.rbs +14 -4
  52. data/core/refinement.rbs +44 -0
  53. data/core/regexp.rbs +156 -14
  54. data/core/ruby_vm.rbs +42 -3
  55. data/core/signal.rbs +78 -39
  56. data/core/string.rbs +2123 -567
  57. data/core/string_io.rbs +204 -0
  58. data/core/struct.rbs +283 -28
  59. data/core/symbol.rbs +304 -30
  60. data/core/thread.rbs +1288 -688
  61. data/core/thread_group.rbs +66 -10
  62. data/core/time.rbs +643 -217
  63. data/core/trace_point.rbs +100 -12
  64. data/core/true_class.rbs +24 -0
  65. data/core/unbound_method.rbs +73 -7
  66. data/core/warning.rbs +37 -12
  67. data/docs/CONTRIBUTING.md +40 -34
  68. data/docs/stdlib.md +3 -102
  69. data/lib/rbs/annotate/annotations.rb +197 -0
  70. data/lib/rbs/annotate/formatter.rb +80 -0
  71. data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
  72. data/lib/rbs/annotate/rdoc_source.rb +120 -0
  73. data/lib/rbs/annotate.rb +6 -0
  74. data/lib/rbs/cli.rb +45 -1
  75. data/lib/rbs/definition_builder.rb +5 -1
  76. data/lib/rbs/location_aux.rb +12 -0
  77. data/lib/rbs/prototype/rb.rb +12 -0
  78. data/lib/rbs/version.rb +1 -1
  79. data/sig/annotate/annotations.rbs +102 -0
  80. data/sig/annotate/formatter.rbs +24 -0
  81. data/sig/annotate/rdoc_annotater.rbs +82 -0
  82. data/sig/annotate/rdoc_source.rbs +30 -0
  83. data/sig/cli.rbs +2 -0
  84. data/sig/collection/{collections.rbs → sources.rbs} +0 -0
  85. data/sig/location.rbs +6 -0
  86. data/sig/method_types.rbs +5 -1
  87. data/sig/polyfill.rbs +78 -0
  88. data/stdlib/abbrev/0/abbrev.rbs +6 -0
  89. data/stdlib/abbrev/0/array.rbs +26 -0
  90. data/stdlib/base64/0/base64.rbs +31 -0
  91. data/stdlib/benchmark/0/benchmark.rbs +74 -3
  92. data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
  93. data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
  94. data/stdlib/cgi/0/core.rbs +59 -0
  95. data/stdlib/coverage/0/coverage.rbs +164 -2
  96. data/stdlib/csv/0/csv.rbs +2862 -398
  97. data/stdlib/date/0/date.rbs +483 -25
  98. data/stdlib/date/0/date_time.rbs +187 -12
  99. data/stdlib/dbm/0/dbm.rbs +152 -17
  100. data/stdlib/digest/0/digest.rbs +146 -0
  101. data/stdlib/erb/0/erb.rbs +65 -245
  102. data/stdlib/fiber/0/fiber.rbs +73 -91
  103. data/stdlib/fileutils/0/fileutils.rbs +301 -1
  104. data/stdlib/find/0/find.rbs +9 -0
  105. data/stdlib/forwardable/0/forwardable.rbs +65 -1
  106. data/stdlib/io-console/0/io-console.rbs +227 -15
  107. data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
  108. data/stdlib/json/0/json.rbs +1146 -144
  109. data/stdlib/logger/0/formatter.rbs +24 -0
  110. data/stdlib/logger/0/log_device.rbs +64 -0
  111. data/stdlib/logger/0/logger.rbs +165 -13
  112. data/stdlib/logger/0/period.rbs +10 -0
  113. data/stdlib/logger/0/severity.rbs +26 -0
  114. data/stdlib/monitor/0/monitor.rbs +163 -0
  115. data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
  116. data/stdlib/net-http/0/net-http.rbs +1492 -683
  117. data/stdlib/nkf/0/nkf.rbs +372 -0
  118. data/stdlib/objspace/0/objspace.rbs +149 -90
  119. data/stdlib/openssl/0/openssl.rbs +8108 -71
  120. data/stdlib/optparse/0/optparse.rbs +487 -19
  121. data/stdlib/pathname/0/pathname.rbs +425 -124
  122. data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
  123. data/stdlib/prime/0/integer-extension.rbs +20 -2
  124. data/stdlib/prime/0/prime.rbs +88 -21
  125. data/stdlib/pstore/0/pstore.rbs +102 -0
  126. data/stdlib/pty/0/pty.rbs +64 -14
  127. data/stdlib/resolv/0/resolv.rbs +420 -31
  128. data/stdlib/rubygems/0/basic_specification.rbs +4 -1
  129. data/stdlib/rubygems/0/config_file.rbs +33 -1
  130. data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
  131. data/stdlib/rubygems/0/installer.rbs +13 -1
  132. data/stdlib/rubygems/0/path_support.rbs +4 -1
  133. data/stdlib/rubygems/0/platform.rbs +5 -1
  134. data/stdlib/rubygems/0/request_set.rbs +44 -2
  135. data/stdlib/rubygems/0/requirement.rbs +65 -2
  136. data/stdlib/rubygems/0/rubygems.rbs +407 -0
  137. data/stdlib/rubygems/0/source_list.rbs +13 -0
  138. data/stdlib/rubygems/0/specification.rbs +21 -1
  139. data/stdlib/rubygems/0/stream_ui.rbs +3 -1
  140. data/stdlib/rubygems/0/uninstaller.rbs +8 -1
  141. data/stdlib/rubygems/0/version.rbs +60 -157
  142. data/stdlib/securerandom/0/securerandom.rbs +44 -0
  143. data/stdlib/set/0/set.rbs +420 -106
  144. data/stdlib/shellwords/0/shellwords.rbs +55 -77
  145. data/stdlib/singleton/0/singleton.rbs +20 -0
  146. data/stdlib/socket/0/addrinfo.rbs +210 -9
  147. data/stdlib/socket/0/basic_socket.rbs +103 -11
  148. data/stdlib/socket/0/ip_socket.rbs +31 -9
  149. data/stdlib/socket/0/socket.rbs +586 -38
  150. data/stdlib/socket/0/tcp_server.rbs +22 -2
  151. data/stdlib/socket/0/tcp_socket.rbs +12 -1
  152. data/stdlib/socket/0/udp_socket.rbs +25 -2
  153. data/stdlib/socket/0/unix_server.rbs +22 -2
  154. data/stdlib/socket/0/unix_socket.rbs +45 -5
  155. data/stdlib/strscan/0/string_scanner.rbs +210 -9
  156. data/stdlib/tempfile/0/tempfile.rbs +58 -10
  157. data/stdlib/time/0/time.rbs +208 -116
  158. data/stdlib/timeout/0/timeout.rbs +10 -0
  159. data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
  160. data/stdlib/tsort/0/cyclic.rbs +1 -0
  161. data/stdlib/tsort/0/interfaces.rbs +1 -0
  162. data/stdlib/tsort/0/tsort.rbs +42 -0
  163. data/stdlib/uri/0/common.rbs +57 -8
  164. data/stdlib/uri/0/file.rbs +55 -109
  165. data/stdlib/uri/0/ftp.rbs +6 -3
  166. data/stdlib/uri/0/generic.rbs +556 -327
  167. data/stdlib/uri/0/http.rbs +26 -115
  168. data/stdlib/uri/0/https.rbs +8 -102
  169. data/stdlib/uri/0/ldap.rbs +143 -137
  170. data/stdlib/uri/0/ldaps.rbs +8 -102
  171. data/stdlib/uri/0/mailto.rbs +3 -0
  172. data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
  173. data/stdlib/uri/0/ws.rbs +6 -3
  174. data/stdlib/uri/0/wss.rbs +5 -3
  175. data/stdlib/yaml/0/dbm.rbs +151 -87
  176. data/stdlib/yaml/0/store.rbs +6 -0
  177. data/stdlib/zlib/0/zlib.rbs +90 -31
  178. metadata +17 -5
  179. data/lib/rbs/location.rb +0 -221
data/stdlib/erb/0/erb.rbs CHANGED
@@ -1,256 +1,20 @@
1
- # # ERB -- Ruby Templating
2
- #
3
- # ## Introduction
4
- #
5
- # ERB provides an easy to use but powerful templating system for Ruby. Using
6
- # ERB, actual Ruby code can be added to any plain text document for the purposes
7
- # of generating document information details and/or flow control.
8
- #
9
- # A very simple example is this:
10
- #
11
- # require 'erb'
12
- #
13
- # x = 42
14
- # template = ERB.new <<-EOF
15
- # The value of x is: <%= x %>
16
- # EOF
17
- # puts template.result(binding)
18
- #
19
- # *Prints:* The value of x is: 42
20
- #
21
- # More complex examples are given below.
22
- #
23
- # ## Recognized Tags
24
- #
25
- # ERB recognizes certain tags in the provided template and converts them based
26
- # on the rules below:
27
- #
28
- # <% Ruby code -- inline with output %>
29
- # <%= Ruby expression -- replace with result %>
30
- # <%# comment -- ignored -- useful in testing %>
31
- # % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
32
- # %% replaced with % if first thing on a line and % processing is used
33
- # <%% or %%> -- replace with <% or %> respectively
34
- #
35
- # All other text is passed through ERB filtering unchanged.
36
- #
37
- # ## Options
38
- #
39
- # There are several settings you can change when you use ERB:
40
- # * the nature of the tags that are recognized;
41
- # * the value of `$SAFE` under which the template is run;
42
- # * the binding used to resolve local variables in the template.
43
- #
44
- #
45
- # See the ERB.new and ERB#result methods for more detail.
46
- #
47
- # ## Character encodings
48
- #
49
- # ERB (or Ruby code generated by ERB) returns a string in the same character
50
- # encoding as the input string. When the input string has a magic comment,
51
- # however, it returns a string in the encoding specified by the magic comment.
52
- #
53
- # # -*- coding: utf-8 -*-
54
- # require 'erb'
55
- #
56
- # template = ERB.new <<EOF
57
- # <%#-*- coding: Big5 -*-%>
58
- # \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
59
- # EOF
60
- # puts template.result
61
- #
62
- # *Prints:* _*ENCODING*_ is Big5.
63
- #
64
- # ## Examples
65
- #
66
- # ### Plain Text
67
- #
68
- # ERB is useful for any generic templating situation. Note that in this
69
- # example, we use the convenient "% at start of line" tag, and we quote the
70
- # template literally with `%q{...}` to avoid trouble with the backslash.
71
- #
72
- # require "erb"
73
- #
74
- # # Create template.
75
- # template = %q{
76
- # From: James Edward Gray II <james@grayproductions.net>
77
- # To: <%= to %>
78
- # Subject: Addressing Needs
79
- #
80
- # <%= to[/\w+/] %>:
81
- #
82
- # Just wanted to send a quick note assuring that your needs are being
83
- # addressed.
84
- #
85
- # I want you to know that my team will keep working on the issues,
86
- # especially:
87
- #
88
- # <%# ignore numerous minor requests -- focus on priorities %>
89
- # % priorities.each do |priority|
90
- # * <%= priority %>
91
- # % end
92
- #
93
- # Thanks for your patience.
94
- #
95
- # James Edward Gray II
96
- # }.gsub(/^ /, '')
97
- #
98
- # message = ERB.new(template, trim_mode: "%<>")
99
- #
100
- # # Set up template data.
101
- # to = "Community Spokesman <spokesman@ruby_community.org>"
102
- # priorities = [ "Run Ruby Quiz",
103
- # "Document Modules",
104
- # "Answer Questions on Ruby Talk" ]
105
- #
106
- # # Produce result.
107
- # email = message.result
108
- # puts email
109
- #
110
- # *Generates:*
111
- #
112
- # From: James Edward Gray II <james@grayproductions.net>
113
- # To: Community Spokesman <spokesman@ruby_community.org>
114
- # Subject: Addressing Needs
115
- #
116
- # Community:
117
- #
118
- # Just wanted to send a quick note assuring that your needs are being addressed.
119
- #
120
- # I want you to know that my team will keep working on the issues, especially:
121
- #
122
- # * Run Ruby Quiz
123
- # * Document Modules
124
- # * Answer Questions on Ruby Talk
125
- #
126
- # Thanks for your patience.
127
- #
128
- # James Edward Gray II
129
- #
130
- # ### Ruby in HTML
131
- #
132
- # ERB is often used in `.rhtml` files (HTML with embedded Ruby). Notice the
133
- # need in this example to provide a special binding when the template is run, so
134
- # that the instance variables in the Product object can be resolved.
135
- #
136
- # require "erb"
137
- #
138
- # # Build template data class.
139
- # class Product
140
- # def initialize( code, name, desc, cost )
141
- # @code = code
142
- # @name = name
143
- # @desc = desc
144
- # @cost = cost
145
- #
146
- # @features = [ ]
147
- # end
148
- #
149
- # def add_feature( feature )
150
- # @features << feature
151
- # end
152
- #
153
- # # Support templating of member data.
154
- # def get_binding
155
- # binding
156
- # end
157
- #
158
- # # ...
159
- # end
160
- #
161
- # # Create template.
162
- # template = %{
163
- # <html>
164
- # <head><title>Ruby Toys -- <%= @name %></title></head>
165
- # <body>
166
- #
167
- # <h1><%= @name %> (<%= @code %>)</h1>
168
- # <p><%= @desc %></p>
169
- #
170
- # <ul>
171
- # <% @features.each do |f| %>
172
- # <li><b><%= f %></b></li>
173
- # <% end %>
174
- # </ul>
175
- #
176
- # <p>
177
- # <% if @cost < 10 %>
178
- # <b>Only <%= @cost %>!!!</b>
179
- # <% else %>
180
- # Call for a price, today!
181
- # <% end %>
182
- # </p>
183
- #
184
- # </body>
185
- # </html>
186
- # }.gsub(/^ /, '')
187
- #
188
- # rhtml = ERB.new(template)
189
- #
190
- # # Set up template data.
191
- # toy = Product.new( "TZ-1002",
192
- # "Rubysapien",
193
- # "Geek's Best Friend! Responds to Ruby commands...",
194
- # 999.95 )
195
- # toy.add_feature("Listens for verbal commands in the Ruby language!")
196
- # toy.add_feature("Ignores Perl, Java, and all C variants.")
197
- # toy.add_feature("Karate-Chop Action!!!")
198
- # toy.add_feature("Matz signature on left leg.")
199
- # toy.add_feature("Gem studded eyes... Rubies, of course!")
200
- #
201
- # # Produce result.
202
- # rhtml.run(toy.get_binding)
203
- #
204
- # *Generates (some blank lines removed):*
205
- #
206
- # <html>
207
- # <head><title>Ruby Toys -- Rubysapien</title></head>
208
- # <body>
209
- #
210
- # <h1>Rubysapien (TZ-1002)</h1>
211
- # <p>Geek's Best Friend! Responds to Ruby commands...</p>
212
- #
213
- # <ul>
214
- # <li><b>Listens for verbal commands in the Ruby language!</b></li>
215
- # <li><b>Ignores Perl, Java, and all C variants.</b></li>
216
- # <li><b>Karate-Chop Action!!!</b></li>
217
- # <li><b>Matz signature on left leg.</b></li>
218
- # <li><b>Gem studded eyes... Rubies, of course!</b></li>
219
- # </ul>
220
- #
221
- # <p>
222
- # Call for a price, today!
223
- # </p>
224
- #
225
- # </body>
226
- # </html>
227
- #
228
- # ## Notes
229
- #
230
- # There are a variety of templating solutions available in various Ruby
231
- # projects:
232
- # * ERB's big brother, eRuby, works the same but is written in C for speed;
233
- # * Amrita (smart at producing HTML/XML);
234
- # * cs/Template (written in C for speed);
235
- # * RDoc, distributed with Ruby, uses its own template engine, which can be
236
- # reused elsewhere;
237
- # * and others; search [RubyGems.org](https://rubygems.org/) or [The Ruby
238
- # Toolbox](https://www.ruby-toolbox.com/).
239
- #
240
- #
241
- # Rails, the web application framework, uses ERB to create views.
242
- #
243
1
  class ERB
2
+ # <!--
3
+ # rdoc-file=lib/erb.rb
4
+ # - version()
5
+ # -->
244
6
  # Returns revision information for the erb.rb module.
245
7
  #
246
8
  def self.version: () -> String
247
9
 
10
+ # <!--
11
+ # rdoc-file=lib/erb.rb
12
+ # - new(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
13
+ # -->
248
14
  # Constructs a new ERB object with the template specified in *str*.
249
15
  #
250
16
  # An ERB object works by building a chunk of Ruby code that will output the
251
- # completed template when run. If *safe_level* is set to a non-nil value, ERB
252
- # code will be run in a separate thread with **$SAFE** set to the provided
253
- # level.
17
+ # completed template when run.
254
18
  #
255
19
  # If *trim_mode* is passed a String containing one or more of the following
256
20
  # modifiers, ERB will adjust its code generation as listed:
@@ -312,30 +76,70 @@ class ERB
312
76
  #
313
77
  def initialize: (String, ?eoutvar: String, ?trim_mode: Integer | String | NilClass) -> untyped
314
78
 
79
+ # <!-- rdoc-file=lib/erb.rb -->
315
80
  # The Ruby code generated by ERB
316
81
  #
317
82
  def src: () -> String
318
83
 
84
+ # <!-- rdoc-file=lib/erb.rb -->
319
85
  # The encoding to eval
320
86
  #
321
87
  def encoding: () -> Encoding
322
88
 
89
+ # <!-- rdoc-file=lib/erb.rb -->
323
90
  # The optional *filename* argument passed to Kernel#eval when the ERB code is
324
91
  # run
325
92
  #
326
93
  def filename: () -> (String | NilClass)
94
+
95
+ # <!-- rdoc-file=lib/erb.rb -->
96
+ # The optional *filename* argument passed to Kernel#eval when the ERB code is
97
+ # run
98
+ #
327
99
  def filename=: (String | NilClass) -> untyped
328
100
 
101
+ # <!-- rdoc-file=lib/erb.rb -->
329
102
  # The optional *lineno* argument passed to Kernel#eval when the ERB code is run
330
103
  #
331
104
  def lineno: () -> Integer
105
+
106
+ # <!-- rdoc-file=lib/erb.rb -->
107
+ # The optional *lineno* argument passed to Kernel#eval when the ERB code is run
108
+ #
332
109
  def lineno=: (Integer) -> untyped
110
+
111
+ # <!--
112
+ # rdoc-file=lib/erb.rb
113
+ # - location=((filename, lineno))
114
+ # -->
115
+ # Sets optional filename and line number that will be used in ERB code
116
+ # evaluation and error reporting. See also #filename= and #lineno=
117
+ #
118
+ # erb = ERB.new('<%= some_x %>')
119
+ # erb.render
120
+ # # undefined local variable or method `some_x'
121
+ # # from (erb):1
122
+ #
123
+ # erb.location = ['file.erb', 3]
124
+ # # All subsequent error reporting would use new location
125
+ # erb.render
126
+ # # undefined local variable or method `some_x'
127
+ # # from file.erb:4
128
+ #
333
129
  def location=: (Array[String | Integer]) -> untyped
334
130
 
131
+ # <!--
132
+ # rdoc-file=lib/erb.rb
133
+ # - run(b=new_toplevel)
134
+ # -->
335
135
  # Generate results and print them. (see ERB#result)
336
136
  #
337
137
  def run: (?Binding) -> untyped
338
138
 
139
+ # <!--
140
+ # rdoc-file=lib/erb.rb
141
+ # - result(b=new_toplevel)
142
+ # -->
339
143
  # Executes the generated ERB code to produce a completed template, returning the
340
144
  # results of that code. (See ERB::new for details on how this process can be
341
145
  # affected by *safe_level*.)
@@ -345,11 +149,19 @@ class ERB
345
149
  #
346
150
  def result: (?Binding) -> String
347
151
 
152
+ # <!--
153
+ # rdoc-file=lib/erb.rb
154
+ # - result_with_hash(hash)
155
+ # -->
348
156
  # Render a template on a new toplevel binding with local variables specified by
349
157
  # a Hash object.
350
158
  #
351
159
  def result_with_hash: (Hash[untyped, untyped]) -> String
352
160
 
161
+ # <!--
162
+ # rdoc-file=lib/erb.rb
163
+ # - def_method(mod, methodname, fname='(ERB)')
164
+ # -->
353
165
  # Define *methodname* as instance method of *mod* from compiled Ruby source.
354
166
  #
355
167
  # example:
@@ -360,6 +172,10 @@ class ERB
360
172
  #
361
173
  def def_method: (Module, String, ?String) -> untyped
362
174
 
175
+ # <!--
176
+ # rdoc-file=lib/erb.rb
177
+ # - def_module(methodname='erb')
178
+ # -->
363
179
  # Create unnamed module, define *methodname* as instance method of it, and
364
180
  # return it.
365
181
  #
@@ -374,6 +190,10 @@ class ERB
374
190
  #
375
191
  def def_module: (?String) -> Module
376
192
 
193
+ # <!--
194
+ # rdoc-file=lib/erb.rb
195
+ # - def_class(superklass=Object, methodname='result')
196
+ # -->
377
197
  # Define unnamed class which has *methodname* as instance method, and return it.
378
198
  #
379
199
  # example:
@@ -1,117 +1,99 @@
1
- # Fibers are primitives for implementing light weight cooperative concurrency in
2
- # Ruby. Basically they are a means of creating code blocks that can be paused
3
- # and resumed, much like threads. The main difference is that they are never
4
- # preempted and that the scheduling must be done by the programmer and not the
5
- # VM.
6
- #
7
- # As opposed to other stackless light weight concurrency models, each fiber
8
- # comes with a stack. This enables the fiber to be paused from deeply nested
9
- # function calls within the fiber block. See the ruby(1) manpage to configure
10
- # the size of the fiber stack(s).
11
- #
12
- # When a fiber is created it will not run automatically. Rather it must be
13
- # explicitly asked to run using the Fiber#resume method. The code running inside
14
- # the fiber can give up control by calling Fiber.yield in which case it yields
15
- # control back to caller (the caller of the Fiber#resume).
16
- #
17
- # Upon yielding or termination the Fiber returns the value of the last executed
18
- # expression
19
- #
20
- # For instance:
21
- #
22
- # fiber = Fiber.new do
23
- # Fiber.yield 1
24
- # 2
25
- # end
26
- #
27
- # puts fiber.resume
28
- # puts fiber.resume
29
- # puts fiber.resume
30
- #
31
- # *produces*
32
- #
33
- # 1
34
- # 2
35
- # FiberError: dead fiber called
36
- #
37
- # The Fiber#resume method accepts an arbitrary number of parameters, if it is
38
- # the first call to #resume then they will be passed as block arguments.
39
- # Otherwise they will be the return value of the call to Fiber.yield
40
- #
41
- # Example:
42
- #
43
- # fiber = Fiber.new do |first|
44
- # second = Fiber.yield first + 2
45
- # end
46
- #
47
- # puts fiber.resume 10
48
- # puts fiber.resume 1_000_000
49
- # puts fiber.resume "The fiber will be dead before I can cause trouble"
50
- #
51
- # *produces*
52
- #
53
- # 12
54
- # 1000000
55
- # FiberError: dead fiber called
56
- #
1
+ %a{annotate:rdoc:skip}
57
2
  class Fiber
58
- # Returns the current fiber. You need to `require 'fiber'` before using this
59
- # method. If you are not running in the context of a fiber this method will
60
- # return the root fiber.
3
+ # <!--
4
+ # rdoc-file=cont.c
5
+ # - Fiber.current -> fiber
6
+ # -->
7
+ # Returns the current fiber. If you are not running in the context of a fiber
8
+ # this method will return the root fiber.
61
9
  #
62
10
  def self.current: () -> Fiber
63
11
 
64
12
  public
65
13
 
14
+ # <!--
15
+ # rdoc-file=cont.c
16
+ # - fiber.alive? -> true or false
17
+ # -->
66
18
  # Returns true if the fiber can still be resumed (or transferred to). After
67
- # finishing execution of the fiber block this method will always return false.
68
- # You need to `require 'fiber'` before using this method.
19
+ # finishing execution of the fiber block this method will always return `false`.
69
20
  #
70
21
  def alive?: () -> bool
71
22
 
23
+ # <!--
24
+ # rdoc-file=cont.c
25
+ # - fiber.transfer(args, ...) -> obj
26
+ # -->
72
27
  # Transfer control to another fiber, resuming it from where it last stopped or
73
28
  # starting it if it was not resumed before. The calling fiber will be suspended
74
- # much like in a call to Fiber.yield. You need to `require 'fiber'` before using
75
- # this method.
29
+ # much like in a call to Fiber.yield.
76
30
  #
77
- # The fiber which receives the transfer call is treats it much like a resume
78
- # call. Arguments passed to transfer are treated like those passed to resume.
31
+ # The fiber which receives the transfer call treats it much like a resume call.
32
+ # Arguments passed to transfer are treated like those passed to resume.
79
33
  #
80
- # You cannot call `resume` on a fiber that has been transferred to. If you call
81
- # `transfer` on a fiber, and later call `resume` on the the fiber, a
82
- # `FiberError` will be raised. Once you call `transfer` on a fiber, the only way
83
- # to resume processing the fiber is to call `transfer` on it again.
34
+ # The two style of control passing to and from fiber (one is #resume and
35
+ # Fiber::yield, another is #transfer to and from fiber) can't be freely mixed.
36
+ #
37
+ # * If the Fiber's lifecycle had started with transfer, it will never be able
38
+ # to yield or be resumed control passing, only finish or transfer back. (It
39
+ # still can resume other fibers that are allowed to be resumed.)
40
+ # * If the Fiber's lifecycle had started with resume, it can yield or transfer
41
+ # to another Fiber, but can receive control back only the way compatible
42
+ # with the way it was given away: if it had transferred, it only can be
43
+ # transferred back, and if it had yielded, it only can be resumed back.
44
+ # After that, it again can transfer or yield.
45
+ #
46
+ #
47
+ # If those rules are broken FiberError is raised.
48
+ #
49
+ # For an individual Fiber design, yield/resume is easier to use (the Fiber just
50
+ # gives away control, it doesn't need to think about who the control is given
51
+ # to), while transfer is more flexible for complex cases, allowing to build
52
+ # arbitrary graphs of Fibers dependent on each other.
84
53
  #
85
54
  # Example:
86
55
  #
87
- # fiber1 = Fiber.new do
88
- # puts "In Fiber 1"
89
- # Fiber.yield
90
- # puts "In Fiber 1 again"
91
- # end
56
+ # manager = nil # For local var to be visible inside worker block
92
57
  #
93
- # fiber2 = Fiber.new do
94
- # puts "In Fiber 2"
95
- # fiber1.transfer
96
- # puts "Never see this message"
97
- # end
58
+ # # This fiber would be started with transfer
59
+ # # It can't yield, and can't be resumed
60
+ # worker = Fiber.new { |work|
61
+ # puts "Worker: starts"
62
+ # puts "Worker: Performed #{work.inspect}, transferring back"
63
+ # # Fiber.yield # this would raise FiberError: attempt to yield on a not resumed fiber
64
+ # # manager.resume # this would raise FiberError: attempt to resume a resumed fiber (double resume)
65
+ # manager.transfer(work.capitalize)
66
+ # }
98
67
  #
99
- # fiber3 = Fiber.new do
100
- # puts "In Fiber 3"
101
- # end
68
+ # # This fiber would be started with resume
69
+ # # It can yield or transfer, and can be transferred
70
+ # # back or resumed
71
+ # manager = Fiber.new {
72
+ # puts "Manager: starts"
73
+ # puts "Manager: transferring 'something' to worker"
74
+ # result = worker.transfer('something')
75
+ # puts "Manager: worker returned #{result.inspect}"
76
+ # # worker.resume # this would raise FiberError: attempt to resume a transferring fiber
77
+ # Fiber.yield # this is OK, the fiber transferred from and to, now it can yield
78
+ # puts "Manager: finished"
79
+ # }
102
80
  #
103
- # fiber2.resume
104
- # fiber3.resume
105
- # fiber1.resume rescue (p $!)
106
- # fiber1.transfer
81
+ # puts "Starting the manager"
82
+ # manager.resume
83
+ # puts "Resuming the manager"
84
+ # # manager.transfer # this would raise FiberError: attempt to transfer to a yielding fiber
85
+ # manager.resume
107
86
  #
108
87
  # *produces*
109
88
  #
110
- # In Fiber 2
111
- # In Fiber 1
112
- # In Fiber 3
113
- # #<FiberError: cannot resume transferred Fiber>
114
- # In Fiber 1 again
89
+ # Starting the manager
90
+ # Manager: starts
91
+ # Manager: transferring 'something' to worker
92
+ # Worker: starts
93
+ # Worker: Performed "something", transferring back
94
+ # Manager: worker returned "Something"
95
+ # Resuming the manager
96
+ # Manager: finished
115
97
  #
116
98
  def transfer: (*untyped) -> untyped
117
99
  end