rbs 4.1.0.pre.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (574) hide show
  1. checksums.yaml +7 -0
  2. data/.clang-format +75 -0
  3. data/.clangd +2 -0
  4. data/.github/dependabot.yml +24 -0
  5. data/.github/workflows/bundle-update.yml +63 -0
  6. data/.github/workflows/c-check.yml +61 -0
  7. data/.github/workflows/comments.yml +37 -0
  8. data/.github/workflows/dependabot.yml +30 -0
  9. data/.github/workflows/jruby.yml +67 -0
  10. data/.github/workflows/milestone.yml +83 -0
  11. data/.github/workflows/ruby.yml +158 -0
  12. data/.github/workflows/rust.yml +184 -0
  13. data/.github/workflows/truffleruby.yml +54 -0
  14. data/.github/workflows/typecheck.yml +39 -0
  15. data/.github/workflows/wasm.yml +53 -0
  16. data/.github/workflows/windows.yml +49 -0
  17. data/.gitignore +38 -0
  18. data/.rubocop.yml +72 -0
  19. data/BSDL +22 -0
  20. data/CHANGELOG.md +2292 -0
  21. data/COPYING +56 -0
  22. data/README.md +240 -0
  23. data/Rakefile +869 -0
  24. data/Steepfile +53 -0
  25. data/config.yml +913 -0
  26. data/core/array.rbs +4142 -0
  27. data/core/basic_object.rbs +376 -0
  28. data/core/binding.rbs +148 -0
  29. data/core/builtin.rbs +278 -0
  30. data/core/class.rbs +223 -0
  31. data/core/comparable.rbs +192 -0
  32. data/core/complex.rbs +812 -0
  33. data/core/constants.rbs +96 -0
  34. data/core/data.rbs +415 -0
  35. data/core/dir.rbs +993 -0
  36. data/core/encoding.rbs +1368 -0
  37. data/core/enumerable.rbs +2506 -0
  38. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  39. data/core/enumerator/product.rbs +92 -0
  40. data/core/enumerator.rbs +705 -0
  41. data/core/env.rbs +6 -0
  42. data/core/errno.rbs +682 -0
  43. data/core/errors.rbs +789 -0
  44. data/core/exception.rbs +485 -0
  45. data/core/false_class.rbs +82 -0
  46. data/core/fiber.rbs +570 -0
  47. data/core/fiber_error.rbs +11 -0
  48. data/core/file.rbs +2045 -0
  49. data/core/file_constants.rbs +463 -0
  50. data/core/file_stat.rbs +534 -0
  51. data/core/file_test.rbs +331 -0
  52. data/core/float.rbs +1316 -0
  53. data/core/gc.rbs +788 -0
  54. data/core/global_variables.rbs +184 -0
  55. data/core/hash.rbs +2183 -0
  56. data/core/integer.rbs +1374 -0
  57. data/core/io/buffer.rbs +995 -0
  58. data/core/io/wait.rbs +48 -0
  59. data/core/io.rbs +3472 -0
  60. data/core/kernel.rbs +3172 -0
  61. data/core/marshal.rbs +207 -0
  62. data/core/match_data.rbs +637 -0
  63. data/core/math.rbs +770 -0
  64. data/core/method.rbs +422 -0
  65. data/core/module.rbs +1856 -0
  66. data/core/nil_class.rbs +210 -0
  67. data/core/numeric.rbs +832 -0
  68. data/core/object.rbs +108 -0
  69. data/core/object_space/weak_key_map.rbs +166 -0
  70. data/core/object_space.rbs +197 -0
  71. data/core/pathname.rbs +1322 -0
  72. data/core/proc.rbs +905 -0
  73. data/core/process.rbs +2316 -0
  74. data/core/ractor.rbs +924 -0
  75. data/core/random.rbs +255 -0
  76. data/core/range.rbs +1209 -0
  77. data/core/rational.rbs +502 -0
  78. data/core/rb_config.rbs +88 -0
  79. data/core/rbs/ops.rbs +154 -0
  80. data/core/rbs/unnamed/argf.rbs +1236 -0
  81. data/core/rbs/unnamed/env_class.rbs +1214 -0
  82. data/core/rbs/unnamed/main_class.rbs +123 -0
  83. data/core/rbs/unnamed/random.rbs +186 -0
  84. data/core/refinement.rbs +59 -0
  85. data/core/regexp.rbs +1974 -0
  86. data/core/ruby.rbs +53 -0
  87. data/core/ruby_vm.rbs +809 -0
  88. data/core/rubygems/basic_specification.rbs +6 -0
  89. data/core/rubygems/config_file.rbs +38 -0
  90. data/core/rubygems/dependency_installer.rbs +6 -0
  91. data/core/rubygems/errors.rbs +109 -0
  92. data/core/rubygems/installer.rbs +15 -0
  93. data/core/rubygems/path_support.rbs +6 -0
  94. data/core/rubygems/platform.rbs +7 -0
  95. data/core/rubygems/request_set.rbs +49 -0
  96. data/core/rubygems/requirement.rbs +148 -0
  97. data/core/rubygems/rubygems.rbs +1105 -0
  98. data/core/rubygems/source_list.rbs +15 -0
  99. data/core/rubygems/specification.rbs +23 -0
  100. data/core/rubygems/stream_ui.rbs +5 -0
  101. data/core/rubygems/uninstaller.rbs +10 -0
  102. data/core/rubygems/version.rbs +293 -0
  103. data/core/set.rbs +751 -0
  104. data/core/signal.rbs +110 -0
  105. data/core/string.rbs +5532 -0
  106. data/core/struct.rbs +668 -0
  107. data/core/symbol.rbs +482 -0
  108. data/core/thread.rbs +1826 -0
  109. data/core/thread_group.rbs +79 -0
  110. data/core/time.rbs +1793 -0
  111. data/core/trace_point.rbs +483 -0
  112. data/core/true_class.rbs +98 -0
  113. data/core/unbound_method.rbs +337 -0
  114. data/core/warning.rbs +87 -0
  115. data/docs/CONTRIBUTING.md +107 -0
  116. data/docs/aliases.md +79 -0
  117. data/docs/architecture.md +110 -0
  118. data/docs/collection.md +192 -0
  119. data/docs/config.md +171 -0
  120. data/docs/data_and_struct.md +86 -0
  121. data/docs/encoding.md +56 -0
  122. data/docs/gem.md +56 -0
  123. data/docs/inline.md +634 -0
  124. data/docs/rbs_by_example.md +309 -0
  125. data/docs/repo.md +125 -0
  126. data/docs/rust.md +96 -0
  127. data/docs/sigs.md +167 -0
  128. data/docs/stdlib.md +147 -0
  129. data/docs/syntax.md +940 -0
  130. data/docs/tools.md +17 -0
  131. data/docs/type_fingerprint.md +21 -0
  132. data/docs/wasm_serialization.md +80 -0
  133. data/exe/rbs +7 -0
  134. data/ext/rbs_extension/ast_translation.c +1855 -0
  135. data/ext/rbs_extension/ast_translation.h +41 -0
  136. data/ext/rbs_extension/class_constants.c +187 -0
  137. data/ext/rbs_extension/class_constants.h +104 -0
  138. data/ext/rbs_extension/compat.h +10 -0
  139. data/ext/rbs_extension/extconf.rb +40 -0
  140. data/ext/rbs_extension/legacy_location.c +294 -0
  141. data/ext/rbs_extension/legacy_location.h +82 -0
  142. data/ext/rbs_extension/main.c +613 -0
  143. data/ext/rbs_extension/rbs_extension.h +16 -0
  144. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  145. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  146. data/goodcheck.yml +91 -0
  147. data/include/rbs/ast.h +1047 -0
  148. data/include/rbs/defines.h +99 -0
  149. data/include/rbs/lexer.h +207 -0
  150. data/include/rbs/location.h +40 -0
  151. data/include/rbs/parser.h +153 -0
  152. data/include/rbs/serialize.h +39 -0
  153. data/include/rbs/string.h +47 -0
  154. data/include/rbs/util/rbs_allocator.h +59 -0
  155. data/include/rbs/util/rbs_assert.h +20 -0
  156. data/include/rbs/util/rbs_buffer.h +83 -0
  157. data/include/rbs/util/rbs_constant_pool.h +155 -0
  158. data/include/rbs/util/rbs_encoding.h +282 -0
  159. data/include/rbs/util/rbs_unescape.h +24 -0
  160. data/include/rbs.h +14 -0
  161. data/lib/rbs/ancestor_graph.rb +92 -0
  162. data/lib/rbs/annotate/annotations.rb +199 -0
  163. data/lib/rbs/annotate/formatter.rb +82 -0
  164. data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
  165. data/lib/rbs/annotate/rdoc_source.rb +131 -0
  166. data/lib/rbs/annotate.rb +8 -0
  167. data/lib/rbs/ast/annotation.rb +29 -0
  168. data/lib/rbs/ast/comment.rb +29 -0
  169. data/lib/rbs/ast/declarations.rb +472 -0
  170. data/lib/rbs/ast/directives.rb +49 -0
  171. data/lib/rbs/ast/members.rb +451 -0
  172. data/lib/rbs/ast/ruby/annotations.rb +451 -0
  173. data/lib/rbs/ast/ruby/comment_block.rb +247 -0
  174. data/lib/rbs/ast/ruby/declarations.rb +291 -0
  175. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  176. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  177. data/lib/rbs/ast/ruby/members.rb +762 -0
  178. data/lib/rbs/ast/type_param.rb +235 -0
  179. data/lib/rbs/ast/visitor.rb +137 -0
  180. data/lib/rbs/buffer.rb +189 -0
  181. data/lib/rbs/builtin_names.rb +58 -0
  182. data/lib/rbs/cli/colored_io.rb +48 -0
  183. data/lib/rbs/cli/diff.rb +84 -0
  184. data/lib/rbs/cli/validate.rb +294 -0
  185. data/lib/rbs/cli.rb +1253 -0
  186. data/lib/rbs/collection/cleaner.rb +38 -0
  187. data/lib/rbs/collection/config/lockfile.rb +92 -0
  188. data/lib/rbs/collection/config/lockfile_generator.rb +269 -0
  189. data/lib/rbs/collection/config.rb +81 -0
  190. data/lib/rbs/collection/installer.rb +32 -0
  191. data/lib/rbs/collection/sources/base.rb +14 -0
  192. data/lib/rbs/collection/sources/git.rb +265 -0
  193. data/lib/rbs/collection/sources/local.rb +81 -0
  194. data/lib/rbs/collection/sources/rubygems.rb +48 -0
  195. data/lib/rbs/collection/sources/stdlib.rb +50 -0
  196. data/lib/rbs/collection/sources.rb +38 -0
  197. data/lib/rbs/collection.rb +16 -0
  198. data/lib/rbs/constant.rb +28 -0
  199. data/lib/rbs/definition.rb +415 -0
  200. data/lib/rbs/definition_builder/ancestor_builder.rb +678 -0
  201. data/lib/rbs/definition_builder/method_builder.rb +295 -0
  202. data/lib/rbs/definition_builder.rb +1054 -0
  203. data/lib/rbs/diff.rb +131 -0
  204. data/lib/rbs/environment/class_entry.rb +69 -0
  205. data/lib/rbs/environment/module_entry.rb +66 -0
  206. data/lib/rbs/environment/use_map.rb +77 -0
  207. data/lib/rbs/environment.rb +1028 -0
  208. data/lib/rbs/environment_loader.rb +167 -0
  209. data/lib/rbs/environment_walker.rb +155 -0
  210. data/lib/rbs/errors.rb +634 -0
  211. data/lib/rbs/factory.rb +18 -0
  212. data/lib/rbs/file_finder.rb +28 -0
  213. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  214. data/lib/rbs/inline_parser.rb +568 -0
  215. data/lib/rbs/location_aux.rb +170 -0
  216. data/lib/rbs/locator.rb +247 -0
  217. data/lib/rbs/method_type.rb +145 -0
  218. data/lib/rbs/namespace.rb +154 -0
  219. data/lib/rbs/parser/lex_result.rb +15 -0
  220. data/lib/rbs/parser/token.rb +23 -0
  221. data/lib/rbs/parser_aux.rb +142 -0
  222. data/lib/rbs/prototype/helpers.rb +197 -0
  223. data/lib/rbs/prototype/node_usage.rb +99 -0
  224. data/lib/rbs/prototype/rb.rb +816 -0
  225. data/lib/rbs/prototype/rbi.rb +625 -0
  226. data/lib/rbs/prototype/runtime/helpers.rb +59 -0
  227. data/lib/rbs/prototype/runtime/reflection.rb +19 -0
  228. data/lib/rbs/prototype/runtime/value_object_generator.rb +279 -0
  229. data/lib/rbs/prototype/runtime.rb +680 -0
  230. data/lib/rbs/repository.rb +127 -0
  231. data/lib/rbs/resolver/constant_resolver.rb +219 -0
  232. data/lib/rbs/resolver/type_name_resolver.rb +167 -0
  233. data/lib/rbs/rewriter.rb +70 -0
  234. data/lib/rbs/sorter.rb +198 -0
  235. data/lib/rbs/source.rb +99 -0
  236. data/lib/rbs/substitution.rb +83 -0
  237. data/lib/rbs/subtractor.rb +204 -0
  238. data/lib/rbs/test/errors.rb +80 -0
  239. data/lib/rbs/test/guaranteed.rb +30 -0
  240. data/lib/rbs/test/hook.rb +212 -0
  241. data/lib/rbs/test/observer.rb +19 -0
  242. data/lib/rbs/test/setup.rb +84 -0
  243. data/lib/rbs/test/setup_helper.rb +50 -0
  244. data/lib/rbs/test/tester.rb +167 -0
  245. data/lib/rbs/test/type_check.rb +457 -0
  246. data/lib/rbs/test.rb +112 -0
  247. data/lib/rbs/type_alias_dependency.rb +100 -0
  248. data/lib/rbs/type_alias_regularity.rb +126 -0
  249. data/lib/rbs/type_name.rb +122 -0
  250. data/lib/rbs/types.rb +1604 -0
  251. data/lib/rbs/unit_test/convertibles.rb +177 -0
  252. data/lib/rbs/unit_test/spy.rb +138 -0
  253. data/lib/rbs/unit_test/type_assertions.rb +383 -0
  254. data/lib/rbs/unit_test/with_aliases.rb +145 -0
  255. data/lib/rbs/unit_test.rb +6 -0
  256. data/lib/rbs/validator.rb +186 -0
  257. data/lib/rbs/variance_calculator.rb +189 -0
  258. data/lib/rbs/vendorer.rb +71 -0
  259. data/lib/rbs/version.rb +5 -0
  260. data/lib/rbs/wasm/deserializer.rb +213 -0
  261. data/lib/rbs/wasm/jars/asm-analysis.jar +0 -0
  262. data/lib/rbs/wasm/jars/asm-commons.jar +0 -0
  263. data/lib/rbs/wasm/jars/asm-tree.jar +0 -0
  264. data/lib/rbs/wasm/jars/asm-util.jar +0 -0
  265. data/lib/rbs/wasm/jars/asm.jar +0 -0
  266. data/lib/rbs/wasm/jars/compiler.jar +0 -0
  267. data/lib/rbs/wasm/jars/log.jar +0 -0
  268. data/lib/rbs/wasm/jars/runtime.jar +0 -0
  269. data/lib/rbs/wasm/jars/wasi.jar +0 -0
  270. data/lib/rbs/wasm/jars/wasm.jar +0 -0
  271. data/lib/rbs/wasm/location.rb +61 -0
  272. data/lib/rbs/wasm/parser.rb +137 -0
  273. data/lib/rbs/wasm/rbs_parser.wasm +0 -0
  274. data/lib/rbs/wasm/runtime.rb +217 -0
  275. data/lib/rbs/wasm/serialization_schema.rb +110 -0
  276. data/lib/rbs/writer.rb +424 -0
  277. data/lib/rbs.rb +117 -0
  278. data/lib/rdoc/discover.rb +20 -0
  279. data/lib/rdoc_plugin/parser.rb +163 -0
  280. data/rbs.gemspec +68 -0
  281. data/schema/annotation.json +14 -0
  282. data/schema/comment.json +26 -0
  283. data/schema/decls.json +326 -0
  284. data/schema/function.json +87 -0
  285. data/schema/location.json +56 -0
  286. data/schema/members.json +266 -0
  287. data/schema/methodType.json +50 -0
  288. data/schema/typeParam.json +52 -0
  289. data/schema/types.json +317 -0
  290. data/sig/ancestor_builder.rbs +163 -0
  291. data/sig/ancestor_graph.rbs +60 -0
  292. data/sig/annotate/annotations.rbs +102 -0
  293. data/sig/annotate/formatter.rbs +24 -0
  294. data/sig/annotate/rdoc_annotater.rbs +85 -0
  295. data/sig/annotate/rdoc_source.rbs +32 -0
  296. data/sig/annotation.rbs +27 -0
  297. data/sig/ast/ruby/annotations.rbs +470 -0
  298. data/sig/ast/ruby/comment_block.rbs +127 -0
  299. data/sig/ast/ruby/declarations.rbs +158 -0
  300. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  301. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  302. data/sig/ast/ruby/members.rbs +198 -0
  303. data/sig/buffer.rbs +108 -0
  304. data/sig/builtin_names.rbs +44 -0
  305. data/sig/cli/colored_io.rbs +15 -0
  306. data/sig/cli/diff.rbs +15 -0
  307. data/sig/cli/validate.rbs +47 -0
  308. data/sig/cli.rbs +89 -0
  309. data/sig/collection/cleaner.rbs +13 -0
  310. data/sig/collection/config/lockfile.rbs +74 -0
  311. data/sig/collection/config/lockfile_generator.rbs +68 -0
  312. data/sig/collection/config.rbs +46 -0
  313. data/sig/collection/installer.rbs +17 -0
  314. data/sig/collection/sources.rbs +214 -0
  315. data/sig/collection.rbs +4 -0
  316. data/sig/comment.rbs +26 -0
  317. data/sig/constant.rbs +21 -0
  318. data/sig/declarations.rbs +274 -0
  319. data/sig/definition.rbs +232 -0
  320. data/sig/definition_builder.rbs +181 -0
  321. data/sig/diff.rbs +28 -0
  322. data/sig/directives.rbs +77 -0
  323. data/sig/environment/class_entry.rbs +50 -0
  324. data/sig/environment/module_entry.rbs +50 -0
  325. data/sig/environment.rbs +286 -0
  326. data/sig/environment_loader.rbs +111 -0
  327. data/sig/environment_walker.rbs +65 -0
  328. data/sig/errors.rbs +408 -0
  329. data/sig/factory.rbs +5 -0
  330. data/sig/file_finder.rbs +28 -0
  331. data/sig/inline_parser/comment_association.rbs +71 -0
  332. data/sig/inline_parser.rbs +126 -0
  333. data/sig/location.rbs +135 -0
  334. data/sig/locator.rbs +56 -0
  335. data/sig/manifest.yaml +5 -0
  336. data/sig/members.rbs +258 -0
  337. data/sig/method_builder.rbs +89 -0
  338. data/sig/method_types.rbs +58 -0
  339. data/sig/namespace.rbs +161 -0
  340. data/sig/parser.rbs +164 -0
  341. data/sig/prototype/helpers.rbs +29 -0
  342. data/sig/prototype/node_usage.rbs +20 -0
  343. data/sig/prototype/rb.rbs +96 -0
  344. data/sig/prototype/rbi.rbs +75 -0
  345. data/sig/prototype/runtime.rbs +182 -0
  346. data/sig/rbs.rbs +21 -0
  347. data/sig/rdoc/rbs.rbs +67 -0
  348. data/sig/repository.rbs +85 -0
  349. data/sig/resolver/constant_resolver.rbs +92 -0
  350. data/sig/resolver/context.rbs +34 -0
  351. data/sig/resolver/type_name_resolver.rbs +61 -0
  352. data/sig/rewriter.rbs +45 -0
  353. data/sig/shims/bundler.rbs +38 -0
  354. data/sig/shims/enumerable.rbs +5 -0
  355. data/sig/shims/rubygems.rbs +19 -0
  356. data/sig/sorter.rbs +41 -0
  357. data/sig/source.rbs +48 -0
  358. data/sig/substitution.rbs +48 -0
  359. data/sig/subtractor.rbs +37 -0
  360. data/sig/test/errors.rbs +52 -0
  361. data/sig/test/guranteed.rbs +9 -0
  362. data/sig/test/type_check.rbs +19 -0
  363. data/sig/test.rbs +82 -0
  364. data/sig/type_alias_dependency.rbs +53 -0
  365. data/sig/type_alias_regularity.rbs +98 -0
  366. data/sig/type_param.rbs +115 -0
  367. data/sig/typename.rbs +89 -0
  368. data/sig/types.rbs +578 -0
  369. data/sig/unit_test/convertibles.rbs +154 -0
  370. data/sig/unit_test/spy.rbs +22 -0
  371. data/sig/unit_test/type_assertions.rbs +211 -0
  372. data/sig/unit_test/with_aliases.rbs +136 -0
  373. data/sig/use_map.rbs +35 -0
  374. data/sig/util.rbs +9 -0
  375. data/sig/validator.rbs +63 -0
  376. data/sig/variance_calculator.rbs +87 -0
  377. data/sig/vendorer.rbs +51 -0
  378. data/sig/version.rbs +3 -0
  379. data/sig/visitor.rbs +47 -0
  380. data/sig/wasm/deserializer.rbs +66 -0
  381. data/sig/wasm/serialization_schema.rbs +13 -0
  382. data/sig/writer.rbs +127 -0
  383. data/src/ast.c +1628 -0
  384. data/src/lexer.c +3217 -0
  385. data/src/lexer.re +155 -0
  386. data/src/lexstate.c +217 -0
  387. data/src/location.c +31 -0
  388. data/src/parser.c +4255 -0
  389. data/src/serialize.c +958 -0
  390. data/src/string.c +41 -0
  391. data/src/util/rbs_allocator.c +162 -0
  392. data/src/util/rbs_assert.c +19 -0
  393. data/src/util/rbs_buffer.c +54 -0
  394. data/src/util/rbs_constant_pool.c +268 -0
  395. data/src/util/rbs_encoding.c +21308 -0
  396. data/src/util/rbs_unescape.c +167 -0
  397. data/stdlib/abbrev/0/abbrev.rbs +66 -0
  398. data/stdlib/abbrev/0/array.rbs +26 -0
  399. data/stdlib/base64/0/base64.rbs +355 -0
  400. data/stdlib/benchmark/0/benchmark.rbs +452 -0
  401. data/stdlib/bigdecimal/0/big_decimal.rbs +1647 -0
  402. data/stdlib/bigdecimal-math/0/big_math.rbs +280 -0
  403. data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
  404. data/stdlib/cgi/0/core.rbs +911 -0
  405. data/stdlib/cgi/0/manifest.yaml +4 -0
  406. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  407. data/stdlib/coverage/0/coverage.rbs +266 -0
  408. data/stdlib/csv/0/csv.rbs +3776 -0
  409. data/stdlib/csv/0/manifest.yaml +3 -0
  410. data/stdlib/date/0/date.rbs +1598 -0
  411. data/stdlib/date/0/date_time.rbs +617 -0
  412. data/stdlib/date/0/time.rbs +26 -0
  413. data/stdlib/dbm/0/dbm.rbs +421 -0
  414. data/stdlib/delegate/0/delegator.rbs +187 -0
  415. data/stdlib/delegate/0/kernel.rbs +47 -0
  416. data/stdlib/delegate/0/simple_delegator.rbs +96 -0
  417. data/stdlib/did_you_mean/0/did_you_mean.rbs +344 -0
  418. data/stdlib/digest/0/digest.rbs +687 -0
  419. data/stdlib/erb/0/erb.rbs +933 -0
  420. data/stdlib/etc/0/etc.rbs +884 -0
  421. data/stdlib/fileutils/0/fileutils.rbs +1753 -0
  422. data/stdlib/find/0/find.rbs +49 -0
  423. data/stdlib/forwardable/0/forwardable.rbs +271 -0
  424. data/stdlib/io-console/0/io-console.rbs +414 -0
  425. data/stdlib/ipaddr/0/ipaddr.rbs +436 -0
  426. data/stdlib/json/0/json.rbs +1963 -0
  427. data/stdlib/kconv/0/kconv.rbs +166 -0
  428. data/stdlib/logger/0/formatter.rbs +45 -0
  429. data/stdlib/logger/0/log_device.rbs +100 -0
  430. data/stdlib/logger/0/logger.rbs +796 -0
  431. data/stdlib/logger/0/manifest.yaml +2 -0
  432. data/stdlib/logger/0/period.rbs +17 -0
  433. data/stdlib/logger/0/severity.rbs +34 -0
  434. data/stdlib/minitest/0/kernel.rbs +42 -0
  435. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +52 -0
  436. data/stdlib/minitest/0/minitest/assertion.rbs +17 -0
  437. data/stdlib/minitest/0/minitest/assertions.rbs +590 -0
  438. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +23 -0
  439. data/stdlib/minitest/0/minitest/bench_spec.rbs +102 -0
  440. data/stdlib/minitest/0/minitest/benchmark.rbs +259 -0
  441. data/stdlib/minitest/0/minitest/composite_reporter.rbs +25 -0
  442. data/stdlib/minitest/0/minitest/compress.rbs +13 -0
  443. data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
  444. data/stdlib/minitest/0/minitest/expectation.rbs +2 -0
  445. data/stdlib/minitest/0/minitest/expectations.rbs +21 -0
  446. data/stdlib/minitest/0/minitest/guard.rbs +64 -0
  447. data/stdlib/minitest/0/minitest/mock.rbs +64 -0
  448. data/stdlib/minitest/0/minitest/parallel/executor.rbs +46 -0
  449. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +5 -0
  450. data/stdlib/minitest/0/minitest/parallel/test.rbs +3 -0
  451. data/stdlib/minitest/0/minitest/parallel.rbs +2 -0
  452. data/stdlib/minitest/0/minitest/pride_io.rbs +62 -0
  453. data/stdlib/minitest/0/minitest/pride_lol.rbs +19 -0
  454. data/stdlib/minitest/0/minitest/progress_reporter.rbs +11 -0
  455. data/stdlib/minitest/0/minitest/reportable.rbs +53 -0
  456. data/stdlib/minitest/0/minitest/reporter.rbs +5 -0
  457. data/stdlib/minitest/0/minitest/result.rbs +28 -0
  458. data/stdlib/minitest/0/minitest/runnable.rbs +163 -0
  459. data/stdlib/minitest/0/minitest/skip.rbs +6 -0
  460. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +48 -0
  461. data/stdlib/minitest/0/minitest/spec/dsl.rbs +129 -0
  462. data/stdlib/minitest/0/minitest/spec.rbs +11 -0
  463. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +81 -0
  464. data/stdlib/minitest/0/minitest/summary_reporter.rbs +18 -0
  465. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +92 -0
  466. data/stdlib/minitest/0/minitest/test.rbs +69 -0
  467. data/stdlib/minitest/0/minitest/unexpected_error.rbs +12 -0
  468. data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
  469. data/stdlib/minitest/0/minitest/unit/test_case.rbs +3 -0
  470. data/stdlib/minitest/0/minitest/unit.rbs +4 -0
  471. data/stdlib/minitest/0/minitest.rbs +115 -0
  472. data/stdlib/monitor/0/monitor.rbs +363 -0
  473. data/stdlib/mutex_m/0/mutex_m.rbs +104 -0
  474. data/stdlib/net-http/0/manifest.yaml +3 -0
  475. data/stdlib/net-http/0/net-http.rbs +5580 -0
  476. data/stdlib/net-protocol/0/manifest.yaml +2 -0
  477. data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
  478. data/stdlib/net-smtp/0/manifest.yaml +2 -0
  479. data/stdlib/net-smtp/0/net-smtp.rbs +55 -0
  480. data/stdlib/nkf/0/nkf.rbs +402 -0
  481. data/stdlib/objspace/0/objspace.rbs +470 -0
  482. data/stdlib/observable/0/observable.rbs +217 -0
  483. data/stdlib/open-uri/0/manifest.yaml +4 -0
  484. data/stdlib/open-uri/0/open-uri.rbs +433 -0
  485. data/stdlib/open3/0/open3.rbs +606 -0
  486. data/stdlib/openssl/0/manifest.yaml +3 -0
  487. data/stdlib/openssl/0/openssl.rbs +12231 -0
  488. data/stdlib/optparse/0/optparse.rbs +1734 -0
  489. data/stdlib/pathname/0/pathname.rbs +36 -0
  490. data/stdlib/pp/0/manifest.yaml +2 -0
  491. data/stdlib/pp/0/pp.rbs +301 -0
  492. data/stdlib/prettyprint/0/prettyprint.rbs +383 -0
  493. data/stdlib/pstore/0/pstore.rbs +608 -0
  494. data/stdlib/psych/0/core_ext.rbs +12 -0
  495. data/stdlib/psych/0/dbm.rbs +237 -0
  496. data/stdlib/psych/0/manifest.yaml +3 -0
  497. data/stdlib/psych/0/psych.rbs +455 -0
  498. data/stdlib/psych/0/store.rbs +57 -0
  499. data/stdlib/pty/0/pty.rbs +240 -0
  500. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  501. data/stdlib/rdoc/0/code_object.rbs +52 -0
  502. data/stdlib/rdoc/0/comment.rbs +61 -0
  503. data/stdlib/rdoc/0/context.rbs +153 -0
  504. data/stdlib/rdoc/0/markup.rbs +117 -0
  505. data/stdlib/rdoc/0/options.rbs +76 -0
  506. data/stdlib/rdoc/0/parser.rbs +56 -0
  507. data/stdlib/rdoc/0/rdoc.rbs +393 -0
  508. data/stdlib/rdoc/0/ri.rbs +17 -0
  509. data/stdlib/rdoc/0/store.rbs +48 -0
  510. data/stdlib/rdoc/0/top_level.rbs +97 -0
  511. data/stdlib/resolv/0/manifest.yaml +3 -0
  512. data/stdlib/resolv/0/resolv.rbs +1787 -0
  513. data/stdlib/ripper/0/ripper.rbs +1654 -0
  514. data/stdlib/securerandom/0/manifest.yaml +2 -0
  515. data/stdlib/securerandom/0/securerandom.rbs +49 -0
  516. data/stdlib/shellwords/0/shellwords.rbs +229 -0
  517. data/stdlib/singleton/0/singleton.rbs +134 -0
  518. data/stdlib/socket/0/addrinfo.rbs +666 -0
  519. data/stdlib/socket/0/basic_socket.rbs +590 -0
  520. data/stdlib/socket/0/constants.rbs +2295 -0
  521. data/stdlib/socket/0/ip_socket.rbs +94 -0
  522. data/stdlib/socket/0/socket.rbs +4170 -0
  523. data/stdlib/socket/0/socket_error.rbs +5 -0
  524. data/stdlib/socket/0/tcp_server.rbs +192 -0
  525. data/stdlib/socket/0/tcp_socket.rbs +87 -0
  526. data/stdlib/socket/0/udp_socket.rbs +133 -0
  527. data/stdlib/socket/0/unix_server.rbs +169 -0
  528. data/stdlib/socket/0/unix_socket.rbs +172 -0
  529. data/stdlib/stringio/0/stringio.rbs +1681 -0
  530. data/stdlib/strscan/0/string_scanner.rbs +1648 -0
  531. data/stdlib/tempfile/0/tempfile.rbs +483 -0
  532. data/stdlib/time/0/time.rbs +434 -0
  533. data/stdlib/timeout/0/timeout.rbs +137 -0
  534. data/stdlib/tmpdir/0/tmpdir.rbs +69 -0
  535. data/stdlib/tsort/0/cyclic.rbs +8 -0
  536. data/stdlib/tsort/0/interfaces.rbs +20 -0
  537. data/stdlib/tsort/0/tsort.rbs +410 -0
  538. data/stdlib/uri/0/common.rbs +621 -0
  539. data/stdlib/uri/0/file.rbs +118 -0
  540. data/stdlib/uri/0/ftp.rbs +13 -0
  541. data/stdlib/uri/0/generic.rbs +1116 -0
  542. data/stdlib/uri/0/http.rbs +104 -0
  543. data/stdlib/uri/0/https.rbs +14 -0
  544. data/stdlib/uri/0/ldap.rbs +230 -0
  545. data/stdlib/uri/0/ldaps.rbs +14 -0
  546. data/stdlib/uri/0/mailto.rbs +92 -0
  547. data/stdlib/uri/0/rfc2396_parser.rbs +189 -0
  548. data/stdlib/uri/0/rfc3986_parser.rbs +2 -0
  549. data/stdlib/uri/0/ws.rbs +13 -0
  550. data/stdlib/uri/0/wss.rbs +9 -0
  551. data/stdlib/yaml/0/manifest.yaml +2 -0
  552. data/stdlib/yaml/0/yaml.rbs +1 -0
  553. data/stdlib/zlib/0/buf_error.rbs +10 -0
  554. data/stdlib/zlib/0/data_error.rbs +10 -0
  555. data/stdlib/zlib/0/deflate.rbs +211 -0
  556. data/stdlib/zlib/0/error.rbs +20 -0
  557. data/stdlib/zlib/0/gzip_file/crc_error.rbs +12 -0
  558. data/stdlib/zlib/0/gzip_file/error.rbs +23 -0
  559. data/stdlib/zlib/0/gzip_file/length_error.rbs +12 -0
  560. data/stdlib/zlib/0/gzip_file/no_footer.rbs +11 -0
  561. data/stdlib/zlib/0/gzip_file.rbs +156 -0
  562. data/stdlib/zlib/0/gzip_reader.rbs +293 -0
  563. data/stdlib/zlib/0/gzip_writer.rbs +168 -0
  564. data/stdlib/zlib/0/inflate.rbs +180 -0
  565. data/stdlib/zlib/0/mem_error.rbs +10 -0
  566. data/stdlib/zlib/0/need_dict.rbs +13 -0
  567. data/stdlib/zlib/0/stream_end.rbs +11 -0
  568. data/stdlib/zlib/0/stream_error.rbs +11 -0
  569. data/stdlib/zlib/0/version_error.rbs +11 -0
  570. data/stdlib/zlib/0/zlib.rbs +449 -0
  571. data/stdlib/zlib/0/zstream.rbs +201 -0
  572. data/wasm/README.md +59 -0
  573. data/wasm/rbs_wasm.c +411 -0
  574. metadata +660 -0
@@ -0,0 +1,2506 @@
1
+ # <!-- rdoc-file=enum.c -->
2
+ # ## What's Here
3
+ #
4
+ # Module Enumerable provides methods that are useful to a collection class for:
5
+ #
6
+ # * [Querying](rdoc-ref:Enumerable@Methods+for+Querying)
7
+ # * [Fetching](rdoc-ref:Enumerable@Methods+for+Fetching)
8
+ # * [Searching and
9
+ # Filtering](rdoc-ref:Enumerable@Methods+for+Searching+and+Filtering)
10
+ # * [Sorting](rdoc-ref:Enumerable@Methods+for+Sorting)
11
+ # * [Iterating](rdoc-ref:Enumerable@Methods+for+Iterating)
12
+ # * [And more....](rdoc-ref:Enumerable@Other+Methods)
13
+ #
14
+ # ### Methods for Querying
15
+ #
16
+ # These methods return information about the Enumerable other than the elements
17
+ # themselves:
18
+ #
19
+ # * #member? (aliased as #include?): Returns `true` if <code>self ==
20
+ # object</code>, `false` otherwise.
21
+ # * #all?: Returns `true` if all elements meet a specified criterion; `false`
22
+ # otherwise.
23
+ # * #any?: Returns `true` if any element meets a specified criterion; `false`
24
+ # otherwise.
25
+ # * #none?: Returns `true` if no element meets a specified criterion; `false`
26
+ # otherwise.
27
+ # * #one?: Returns `true` if exactly one element meets a specified criterion;
28
+ # `false` otherwise.
29
+ # * #count: Returns the count of elements, based on an argument or block
30
+ # criterion, if given.
31
+ # * #tally: Returns a new Hash containing the counts of occurrences of each
32
+ # element.
33
+ #
34
+ # ### Methods for Fetching
35
+ #
36
+ # These methods return entries from the Enumerable, without modifying it:
37
+ #
38
+ # <em>Leading, trailing, or all elements</em>:
39
+ #
40
+ # * #to_a (aliased as #entries): Returns all elements.
41
+ # * #first: Returns the first element or leading elements.
42
+ # * #take: Returns a specified number of leading elements.
43
+ # * #drop: Returns a specified number of trailing elements.
44
+ # * #take_while: Returns leading elements as specified by the given block.
45
+ # * #drop_while: Returns trailing elements as specified by the given block.
46
+ #
47
+ # *Minimum and maximum value elements*:
48
+ #
49
+ # * #min: Returns the elements whose values are smallest among the elements,
50
+ # as determined by <code>#<=></code> or a given block.
51
+ # * #max: Returns the elements whose values are largest among the elements, as
52
+ # determined by <code>#<=></code> or a given block.
53
+ # * #minmax: Returns a 2-element Array containing the smallest and largest
54
+ # elements.
55
+ # * #min_by: Returns the smallest element, as determined by the given block.
56
+ # * #max_by: Returns the largest element, as determined by the given block.
57
+ # * #minmax_by: Returns the smallest and largest elements, as determined by
58
+ # the given block.
59
+ #
60
+ # <em>Groups, slices, and partitions</em>:
61
+ #
62
+ # * #group_by: Returns a Hash that partitions the elements into groups.
63
+ # * #partition: Returns elements partitioned into two new Arrays, as
64
+ # determined by the given block.
65
+ # * #slice_after: Returns a new Enumerator whose entries are a partition of
66
+ # `self`, based either on a given `object` or a given block.
67
+ # * #slice_before: Returns a new Enumerator whose entries are a partition of
68
+ # `self`, based either on a given `object` or a given block.
69
+ # * #slice_when: Returns a new Enumerator whose entries are a partition of
70
+ # `self` based on the given block.
71
+ # * #chunk: Returns elements organized into chunks as specified by the given
72
+ # block.
73
+ # * #chunk_while: Returns elements organized into chunks as specified by the
74
+ # given block.
75
+ #
76
+ # ### Methods for Searching and Filtering
77
+ #
78
+ # These methods return elements that meet a specified criterion:
79
+ #
80
+ # * #find (aliased as #detect): Returns an element selected by the block.
81
+ # * #find_all (aliased as #filter, #select): Returns elements selected by the
82
+ # block.
83
+ # * #find_index: Returns the index of an element selected by a given object or
84
+ # block.
85
+ # * #reject: Returns elements not rejected by the block.
86
+ # * #uniq: Returns elements that are not duplicates.
87
+ #
88
+ # ### Methods for Sorting
89
+ #
90
+ # These methods return elements in sorted order:
91
+ #
92
+ # * #sort: Returns the elements, sorted by <code>#<=></code> or the given
93
+ # block.
94
+ # * #sort_by: Returns the elements, sorted by the given block.
95
+ #
96
+ # ### Methods for Iterating
97
+ #
98
+ # * #each_entry: Calls the block with each successive element (slightly
99
+ # different from #each).
100
+ # * #each_with_index: Calls the block with each successive element and its
101
+ # index.
102
+ # * #each_with_object: Calls the block with each successive element and a
103
+ # given object.
104
+ # * #each_slice: Calls the block with successive non-overlapping slices.
105
+ # * #each_cons: Calls the block with successive overlapping slices. (different
106
+ # from #each_slice).
107
+ # * #reverse_each: Calls the block with each successive element, in reverse
108
+ # order.
109
+ #
110
+ # ### Other Methods
111
+ #
112
+ # * #collect (aliased as #map): Returns objects returned by the block.
113
+ # * #filter_map: Returns truthy objects returned by the block.
114
+ # * #flat_map (aliased as #collect_concat): Returns flattened objects returned
115
+ # by the block.
116
+ # * #grep: Returns elements selected by a given object or objects returned by
117
+ # a given block.
118
+ # * #grep_v: Returns elements not selected by a given object or objects
119
+ # returned by a given block.
120
+ # * #inject (aliased as #reduce): Returns the object formed by combining all
121
+ # elements.
122
+ # * #sum: Returns the sum of the elements, using method <code>+</code>.
123
+ # * #zip: Combines each element with elements from other enumerables; returns
124
+ # the n-tuples or calls the block with each.
125
+ # * #cycle: Calls the block with each element, cycling repeatedly.
126
+ #
127
+ # ## Usage
128
+ #
129
+ # To use module Enumerable in a collection class:
130
+ #
131
+ # * Include it:
132
+ #
133
+ # include Enumerable
134
+ #
135
+ # * Implement method <code>#each</code> which must yield successive elements
136
+ # of the collection. The method will be called by almost any Enumerable
137
+ # method.
138
+ #
139
+ # Example:
140
+ #
141
+ # class Foo
142
+ # include Enumerable
143
+ # def each
144
+ # yield 1
145
+ # yield 1, 2
146
+ # yield
147
+ # end
148
+ # end
149
+ # Foo.new.each_entry{ |element| p element }
150
+ #
151
+ # Output:
152
+ #
153
+ # 1
154
+ # [1, 2]
155
+ # nil
156
+ #
157
+ # ## Enumerable in Ruby Classes
158
+ #
159
+ # These Ruby core classes include (or extend) Enumerable:
160
+ #
161
+ # * ARGF
162
+ # * Array
163
+ # * Dir
164
+ # * Enumerator
165
+ # * ENV (extends)
166
+ # * Hash
167
+ # * IO
168
+ # * Range
169
+ # * Struct
170
+ #
171
+ # These Ruby standard library classes include Enumerable:
172
+ #
173
+ # * CSV
174
+ # * CSV::Table
175
+ # * CSV::Row
176
+ # * Set
177
+ #
178
+ # Virtually all methods in Enumerable call method <code>#each</code> in the
179
+ # including class:
180
+ #
181
+ # * <code>Hash#each</code> yields the next key-value pair as a 2-element
182
+ # Array.
183
+ # * <code>Struct#each</code> yields the next name-value pair as a 2-element
184
+ # Array.
185
+ # * For the other classes above, <code>#each</code> yields the next object
186
+ # from the collection.
187
+ #
188
+ # ## About the Examples
189
+ #
190
+ # The example code snippets for the Enumerable methods:
191
+ #
192
+ # * Always show the use of one or more Array-like classes (often Array
193
+ # itself).
194
+ # * Sometimes show the use of a Hash-like class. For some methods, though, the
195
+ # usage would not make sense, and so it is not shown. Example: #tally would
196
+ # find exactly one of each Hash entry.
197
+ #
198
+ # ## Extended Methods
199
+ #
200
+ # A Enumerable class may define extended methods. This section describes the
201
+ # standard behavior of extension methods for reference purposes.
202
+ #
203
+ # ### #size
204
+ #
205
+ # Enumerator has a #size method. It uses the size function argument passed to
206
+ # <code>Enumerator.new</code>.
207
+ #
208
+ # e = Enumerator.new(-> { 3 }) {|y| p y; y.yield :a; y.yield :b; y.yield :c; :z }
209
+ # p e.size #=> 3
210
+ # p e.next #=> :a
211
+ # p e.next #=> :b
212
+ # p e.next #=> :c
213
+ # begin
214
+ # e.next
215
+ # rescue StopIteration
216
+ # p $!.result #=> :z
217
+ # end
218
+ #
219
+ # The result of the size function should represent the number of iterations
220
+ # (i.e., the number of times Enumerator::Yielder#yield is called). In the above
221
+ # example, the block calls #yield three times, and the size function, +-> { 3
222
+ # }+, returns 3 accordingly. The result of the size function can be an integer,
223
+ # <code>Float::INFINITY</code>, or `nil`. An integer means the exact number of
224
+ # times #yield will be called, as shown above. <code>Float::INFINITY</code>
225
+ # indicates an infinite number of #yield calls. `nil` means the number of #yield
226
+ # calls is difficult or impossible to determine.
227
+ #
228
+ # Many iteration methods return an Enumerator object with an appropriate size
229
+ # function if no block is given.
230
+ #
231
+ # Examples:
232
+ #
233
+ # ["a", "b", "c"].each.size #=> 3
234
+ # {a: "x", b: "y", c: "z"}.each.size #=> 3
235
+ # (0..20).to_a.permutation.size #=> 51090942171709440000
236
+ # loop.size #=> Float::INFINITY
237
+ # (1..100).drop_while.size #=> nil # size depends on the block's behavior
238
+ # STDIN.each.size #=> nil # cannot be computed without consuming input
239
+ # File.open("/etc/resolv.conf").each.size #=> nil # cannot be computed without reading the file
240
+ #
241
+ # The behavior of #size for Range-based enumerators depends on the #begin
242
+ # element:
243
+ #
244
+ # * If the #begin element is an Integer, the #size method returns an Integer
245
+ # or <code>Float::INFINITY</code>.
246
+ # * If the #begin element is an object with a #succ method (other than
247
+ # Integer), #size returns `nil`. (Computing the size would require
248
+ # repeatedly calling #succ, which may be too slow.)
249
+ # * If the #begin element does not have a #succ method, #size raises a
250
+ # TypeError.
251
+ #
252
+ # Examples:
253
+ #
254
+ # (10..42).each.size #=> 33
255
+ # (10..42.9).each.size #=> 33 (the #end element may be a non-integer numeric)
256
+ # (10..).each.size #=> Float::INFINITY
257
+ # ("a".."z").each.size #=> nil
258
+ # ("a"..).each.size #=> nil
259
+ # (1.0..9.0).each.size # raises TypeError (Float does not have #succ)
260
+ # (..10).each.size # raises TypeError (beginless range has nil as its #begin)
261
+ #
262
+ # The Enumerable module itself does not define a #size method. A class that
263
+ # includes Enumerable may define its own #size method. It is recommended that
264
+ # such a #size method be consistent with Enumerator#size.
265
+ #
266
+ # Array and Hash implement #size and return values consistent with
267
+ # Enumerator#size. IO and Dir do not define #size, which is also consistent
268
+ # because the corresponding enumerator's size function returns `nil`.
269
+ #
270
+ # However, it is not strictly required for a class's #size method to match
271
+ # Enumerator#size. For example, File#size returns the number of bytes in the
272
+ # file, not the number of lines.
273
+ #
274
+ module Enumerable[unchecked out E] : _Each[E]
275
+ %a{private}
276
+ interface _Pattern
277
+ def ===: (untyped) -> bool
278
+ end
279
+
280
+ # <!--
281
+ # rdoc-file=enum.c
282
+ # - all? -> true or false
283
+ # - all?(pattern) -> true or false
284
+ # - all? {|element| ... } -> true or false
285
+ # -->
286
+ # Returns whether every element meets a given criterion.
287
+ #
288
+ # If `self` has no element, returns `true` and argument or block are not used.
289
+ #
290
+ # With no argument and no block, returns whether every element is truthy:
291
+ #
292
+ # (1..4).all? # => true
293
+ # %w[a b c d].all? # => true
294
+ # [1, 2, nil].all? # => false
295
+ # ['a','b', false].all? # => false
296
+ # [].all? # => true
297
+ #
298
+ # With argument `pattern` and no block, returns whether for each element
299
+ # `element`, <code>pattern === element</code>:
300
+ #
301
+ # (1..4).all?(Integer) # => true
302
+ # (1..4).all?(Numeric) # => true
303
+ # (1..4).all?(Float) # => false
304
+ # %w[bar baz bat bam].all?(/ba/) # => true
305
+ # %w[bar baz bat bam].all?(/bar/) # => false
306
+ # %w[bar baz bat bam].all?('ba') # => false
307
+ # {foo: 0, bar: 1, baz: 2}.all?(Array) # => true
308
+ # {foo: 0, bar: 1, baz: 2}.all?(Hash) # => false
309
+ # [].all?(Integer) # => true
310
+ #
311
+ # With a block given, returns whether the block returns a truthy value for every
312
+ # element:
313
+ #
314
+ # (1..4).all? {|element| element < 5 } # => true
315
+ # (1..4).all? {|element| element < 4 } # => false
316
+ # {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 3 } # => true
317
+ # {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 2 } # => false
318
+ #
319
+ # Related: #any?, #none? #one?.
320
+ #
321
+ def all?: () -> bool
322
+ | (_Pattern) -> bool
323
+ | () { (E) -> boolish } -> bool
324
+
325
+ # <!--
326
+ # rdoc-file=enum.c
327
+ # - any? -> true or false
328
+ # - any?(pattern) -> true or false
329
+ # - any? {|element| ... } -> true or false
330
+ # -->
331
+ # Returns whether any element meets a given criterion.
332
+ #
333
+ # If `self` has no element, returns `false` and argument or block are not used.
334
+ #
335
+ # With no argument and no block, returns whether any element is truthy:
336
+ #
337
+ # (1..4).any? # => true
338
+ # %w[a b c d].any? # => true
339
+ # [1, false, nil].any? # => true
340
+ # [].any? # => false
341
+ #
342
+ # With argument `pattern` and no block, returns whether for any element
343
+ # `element`, <code>pattern === element</code>:
344
+ #
345
+ # [nil, false, 0].any?(Integer) # => true
346
+ # [nil, false, 0].any?(Numeric) # => true
347
+ # [nil, false, 0].any?(Float) # => false
348
+ # %w[bar baz bat bam].any?(/m/) # => true
349
+ # %w[bar baz bat bam].any?(/foo/) # => false
350
+ # %w[bar baz bat bam].any?('ba') # => false
351
+ # {foo: 0, bar: 1, baz: 2}.any?(Array) # => true
352
+ # {foo: 0, bar: 1, baz: 2}.any?(Hash) # => false
353
+ # [].any?(Integer) # => false
354
+ #
355
+ # With a block given, returns whether the block returns a truthy value for any
356
+ # element:
357
+ #
358
+ # (1..4).any? {|element| element < 2 } # => true
359
+ # (1..4).any? {|element| element < 1 } # => false
360
+ # {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 1 } # => true
361
+ # {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 0 } # => false
362
+ #
363
+ # Related: #all?, #none?, #one?.
364
+ #
365
+ def any?: () -> bool
366
+ | (_Pattern) -> bool
367
+ | () { (E) -> boolish } -> bool
368
+
369
+ # <!--
370
+ # rdoc-file=enum.c
371
+ # - map {|element| ... } -> array
372
+ # - map -> enumerator
373
+ # -->
374
+ # Returns an array of objects returned by the block.
375
+ #
376
+ # With a block given, calls the block with successive elements; returns an array
377
+ # of the objects returned by the block:
378
+ #
379
+ # (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
380
+ # {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
381
+ #
382
+ # With no block given, returns an Enumerator.
383
+ #
384
+ def collect: [U] () { (E arg0) -> U } -> ::Array[U]
385
+ | () -> ::Enumerator[E, ::Array[untyped]]
386
+
387
+ # <!-- rdoc-file=enum.c -->
388
+ # Returns an array of flattened objects returned by the block.
389
+ #
390
+ # With a block given, calls the block with successive elements; returns a
391
+ # flattened array of objects returned by the block:
392
+ #
393
+ # [0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3]
394
+ # [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3]
395
+ # [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
396
+ # {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
397
+ #
398
+ # With no block given, returns an Enumerator.
399
+ #
400
+ # Alias: #collect_concat.
401
+ #
402
+ def collect_concat: [U] () { (E) -> (::Array[U] | U) } -> ::Array[U]
403
+ | () -> ::Enumerator[E, ::Array[untyped]]
404
+
405
+ # <!--
406
+ # rdoc-file=enum.c
407
+ # - compact -> array
408
+ # -->
409
+ # Returns an array of all non-`nil` elements:
410
+ #
411
+ # a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
412
+ # a.compact # => [0, "a", false, false, "a", 0]
413
+ #
414
+ def compact: () -> Array[E]
415
+
416
+ # <!--
417
+ # rdoc-file=enum.c
418
+ # - count -> integer
419
+ # - count(object) -> integer
420
+ # - count {|element| ... } -> integer
421
+ # -->
422
+ # Returns the count of elements, based on an argument or block criterion, if
423
+ # given.
424
+ #
425
+ # With no argument and no block given, returns the number of elements:
426
+ #
427
+ # [0, 1, 2].count # => 3
428
+ # {foo: 0, bar: 1, baz: 2}.count # => 3
429
+ #
430
+ # With argument `object` given, returns the number of elements that are
431
+ # <code>==</code> to `object`:
432
+ #
433
+ # [0, 1, 2, 1].count(1) # => 2
434
+ #
435
+ # With a block given, calls the block with each element and returns the number
436
+ # of elements for which the block returns a truthy value:
437
+ #
438
+ # [0, 1, 2, 3].count {|element| element < 2} # => 2
439
+ # {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2
440
+ #
441
+ def count: () -> Integer
442
+ | (E) -> Integer
443
+ | () { (E) -> boolish } -> Integer
444
+
445
+ # <!--
446
+ # rdoc-file=enum.c
447
+ # - cycle(n = nil) {|element| ...} -> nil
448
+ # - cycle(n = nil) -> enumerator
449
+ # -->
450
+ # When called with positive integer argument `n` and a block, calls the block
451
+ # with each element, then does so again, until it has done so `n` times; returns
452
+ # `nil`:
453
+ #
454
+ # a = []
455
+ # (1..4).cycle(3) {|element| a.push(element) } # => nil
456
+ # a # => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
457
+ # a = []
458
+ # ('a'..'d').cycle(2) {|element| a.push(element) }
459
+ # a # => ["a", "b", "c", "d", "a", "b", "c", "d"]
460
+ # a = []
461
+ # {foo: 0, bar: 1, baz: 2}.cycle(2) {|element| a.push(element) }
462
+ # a # => [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]
463
+ #
464
+ # If count is zero or negative, does not call the block.
465
+ #
466
+ # When called with a block and `n` is `nil`, cycles forever.
467
+ #
468
+ # When no block is given, returns an Enumerator.
469
+ #
470
+ def cycle: (?Integer n) { (E arg0) -> untyped } -> NilClass
471
+ | (?Integer n) -> ::Enumerator[E, NilClass]
472
+
473
+ # <!-- rdoc-file=enum.c -->
474
+ # Returns the first element for which the block returns a truthy value.
475
+ #
476
+ # With a block given, calls the block with successive elements of the
477
+ # collection; returns the first element for which the block returns a truthy
478
+ # value:
479
+ #
480
+ # (0..9).find {|element| element > 2} # => 3
481
+ #
482
+ # If no such element is found, calls `if_none_proc` and returns its return
483
+ # value.
484
+ #
485
+ # (0..9).find(proc {false}) {|element| element > 12} # => false
486
+ # {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
487
+ # {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
488
+ #
489
+ # With no block given, returns an Enumerator.
490
+ #
491
+ def detect: (?Proc ifnone) { (E) -> boolish } -> E?
492
+ | (?Proc ifnone) -> ::Enumerator[E, E?]
493
+
494
+ # <!--
495
+ # rdoc-file=enum.c
496
+ # - drop(n) -> array
497
+ # -->
498
+ # For positive integer `n`, returns an array containing all but the first `n`
499
+ # elements:
500
+ #
501
+ # r = (1..4)
502
+ # r.drop(3) # => [4]
503
+ # r.drop(2) # => [3, 4]
504
+ # r.drop(1) # => [2, 3, 4]
505
+ # r.drop(0) # => [1, 2, 3, 4]
506
+ # r.drop(50) # => []
507
+ #
508
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
509
+ # h.drop(2) # => [[:baz, 2], [:bat, 3]]
510
+ #
511
+ def drop: (Integer n) -> ::Array[E]
512
+
513
+ # <!--
514
+ # rdoc-file=enum.c
515
+ # - drop_while {|element| ... } -> array
516
+ # - drop_while -> enumerator
517
+ # -->
518
+ # Calls the block with successive elements as long as the block returns a truthy
519
+ # value; returns an array of all elements after that point:
520
+ #
521
+ # (1..4).drop_while{|i| i < 3 } # => [3, 4]
522
+ # h = {foo: 0, bar: 1, baz: 2}
523
+ # a = h.drop_while{|element| key, value = *element; value < 2 }
524
+ # a # => [[:baz, 2]]
525
+ #
526
+ # With no block given, returns an Enumerator.
527
+ #
528
+ # e = (1..4).drop_while
529
+ # p e #=> #<Enumerator: 1..4:drop_while>
530
+ # i = e.next; p i; e.feed(i < 3) #=> 1
531
+ # i = e.next; p i; e.feed(i < 3) #=> 2
532
+ # i = e.next; p i; e.feed(i < 3) #=> 3
533
+ # begin
534
+ # e.next
535
+ # rescue StopIteration
536
+ # p $!.result #=> [3, 4]
537
+ # end
538
+ #
539
+ def drop_while: () { (E) -> boolish } -> ::Array[E]
540
+ | () -> ::Enumerator[E, ::Array[E]]
541
+
542
+ # <!--
543
+ # rdoc-file=enum.c
544
+ # - each_cons(n) { ... } -> self
545
+ # - each_cons(n) -> enumerator
546
+ # -->
547
+ # Calls the block with each successive overlapped `n`-tuple of elements; returns
548
+ # `self`:
549
+ #
550
+ # a = []
551
+ # (1..5).each_cons(3) {|element| a.push(element) }
552
+ # a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
553
+ #
554
+ # a = []
555
+ # h = {foo: 0, bar: 1, baz: 2, bam: 3}
556
+ # h.each_cons(2) {|element| a.push(element) }
557
+ # a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
558
+ #
559
+ # With no block given, returns an Enumerator.
560
+ #
561
+ def each_cons: (Integer n) { (::Array[E]) -> void } -> self
562
+ | (Integer n) -> ::Enumerator[::Array[E], self]
563
+
564
+ # <!--
565
+ # rdoc-file=enum.c
566
+ # - each_with_index(*args) {|element, i| ..... } -> self
567
+ # - each_with_index(*args) -> enumerator
568
+ # -->
569
+ # Invoke <code>self.each</code> with <code>*args</code>. With a block given, the
570
+ # block receives each element and its index; returns `self`:
571
+ #
572
+ # h = {}
573
+ # (1..4).each_with_index {|element, i| h[element] = i } # => 1..4
574
+ # h # => {1=>0, 2=>1, 3=>2, 4=>3}
575
+ #
576
+ # h = {}
577
+ # %w[a b c d].each_with_index {|element, i| h[element] = i }
578
+ # # => ["a", "b", "c", "d"]
579
+ # h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}
580
+ #
581
+ # a = []
582
+ # h = {foo: 0, bar: 1, baz: 2}
583
+ # h.each_with_index {|element, i| a.push([i, element]) }
584
+ # # => {:foo=>0, :bar=>1, :baz=>2}
585
+ # a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
586
+ #
587
+ # With no block given, returns an Enumerator.
588
+ #
589
+ def each_with_index: () { (E, Integer index) -> untyped } -> self
590
+ | () -> ::Enumerator[[ E, Integer ], self]
591
+
592
+ # <!--
593
+ # rdoc-file=enum.c
594
+ # - each_with_object(object) { |(*args), memo_object| ... } -> object
595
+ # - each_with_object(object) -> enumerator
596
+ # -->
597
+ # Calls the block once for each element, passing both the element and the given
598
+ # object:
599
+ #
600
+ # (1..4).each_with_object([]) {|i, a| a.push(i**2) }
601
+ # # => [1, 4, 9, 16]
602
+ #
603
+ # {foo: 0, bar: 1, baz: 2}.each_with_object({}) {|(k, v), h| h[v] = k }
604
+ # # => {0=>:foo, 1=>:bar, 2=>:baz}
605
+ #
606
+ # With no block given, returns an Enumerator.
607
+ #
608
+ def each_with_object: [U] (U obj) { (E, U obj) -> untyped } -> U
609
+ | [U] (U obj) -> ::Enumerator[[ E, U ], U]
610
+
611
+ # <!-- rdoc-file=enum.c -->
612
+ # Returns an array containing the items in `self`:
613
+ #
614
+ # (0..4).to_a # => [0, 1, 2, 3, 4]
615
+ #
616
+ def entries: () -> ::Array[E]
617
+
618
+ def enum_for: (Symbol method, *untyped, **untyped) ?{ (?) -> Integer } -> Enumerator[untyped, untyped]
619
+ | () ?{ () -> Integer } -> Enumerator[E, self]
620
+
621
+ %a{annotate:rdoc:skip}
622
+ alias to_enum enum_for
623
+
624
+ # <!--
625
+ # rdoc-file=enum.c
626
+ # - select {|element| ... } -> array
627
+ # - select -> enumerator
628
+ # -->
629
+ # Returns an array containing elements selected by the block.
630
+ #
631
+ # With a block given, calls the block with successive elements; returns an array
632
+ # of those elements for which the block returns a truthy value:
633
+ #
634
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
635
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
636
+ # a # => {:bar=>1, :baz=>2}
637
+ #
638
+ # With no block given, returns an Enumerator.
639
+ #
640
+ # Related: #reject.
641
+ #
642
+ def find_all: () { (E) -> boolish } -> ::Array[E]
643
+ | () -> ::Enumerator[E, ::Array[E]]
644
+
645
+ # <!-- rdoc-file=enum.c -->
646
+ # Returns an array containing elements selected by the block.
647
+ #
648
+ # With a block given, calls the block with successive elements; returns an array
649
+ # of those elements for which the block returns a truthy value:
650
+ #
651
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
652
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
653
+ # a # => {:bar=>1, :baz=>2}
654
+ #
655
+ # With no block given, returns an Enumerator.
656
+ #
657
+ # Related: #reject.
658
+ #
659
+ alias select find_all
660
+
661
+ # <!-- rdoc-file=enum.c -->
662
+ # Returns an array containing elements selected by the block.
663
+ #
664
+ # With a block given, calls the block with successive elements; returns an array
665
+ # of those elements for which the block returns a truthy value:
666
+ #
667
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
668
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
669
+ # a # => {:bar=>1, :baz=>2}
670
+ #
671
+ # With no block given, returns an Enumerator.
672
+ #
673
+ # Related: #reject.
674
+ #
675
+ alias filter find_all
676
+
677
+ # <!--
678
+ # rdoc-file=enum.c
679
+ # - find_index(object) -> integer or nil
680
+ # - find_index {|element| ... } -> integer or nil
681
+ # - find_index -> enumerator
682
+ # -->
683
+ # Returns the index of the first element that meets a specified criterion, or
684
+ # `nil` if no such element is found.
685
+ #
686
+ # With argument `object` given, returns the index of the first element that is
687
+ # <code>==</code> `object`:
688
+ #
689
+ # ['a', 'b', 'c', 'b'].find_index('b') # => 1
690
+ #
691
+ # With a block given, calls the block with successive elements; returns the
692
+ # first element for which the block returns a truthy value:
693
+ #
694
+ # ['a', 'b', 'c', 'b'].find_index {|element| element.start_with?('b') } # => 1
695
+ # {foo: 0, bar: 1, baz: 2}.find_index {|key, value| value > 1 } # => 2
696
+ #
697
+ # With no argument and no block given, returns an Enumerator.
698
+ #
699
+ def find_index: (untyped value) -> Integer?
700
+ | () { (E) -> boolish } -> Integer?
701
+ | () -> ::Enumerator[E, Integer?]
702
+
703
+ # <!--
704
+ # rdoc-file=enum.c
705
+ # - first -> element or nil
706
+ # - first(n) -> array
707
+ # -->
708
+ # Returns the first element or elements.
709
+ #
710
+ # With no argument, returns the first element, or `nil` if there is none:
711
+ #
712
+ # (1..4).first # => 1
713
+ # %w[a b c].first # => "a"
714
+ # {foo: 1, bar: 1, baz: 2}.first # => [:foo, 1]
715
+ # [].first # => nil
716
+ #
717
+ # With integer argument `n`, returns an array containing the first `n` elements
718
+ # that exist:
719
+ #
720
+ # (1..4).first(2) # => [1, 2]
721
+ # %w[a b c d].first(3) # => ["a", "b", "c"]
722
+ # %w[a b c d].first(50) # => ["a", "b", "c", "d"]
723
+ # {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]]
724
+ # [].first(2) # => []
725
+ #
726
+ def first: () -> E?
727
+ | (_ToInt n) -> ::Array[E]
728
+
729
+ # <!--
730
+ # rdoc-file=enum.c
731
+ # - grep(pattern) -> array
732
+ # - grep(pattern) {|element| ... } -> array
733
+ # -->
734
+ # Returns an array of objects based elements of `self` that match the given
735
+ # pattern.
736
+ #
737
+ # With no block given, returns an array containing each element for which
738
+ # <code>pattern === element</code> is `true`:
739
+ #
740
+ # a = ['foo', 'bar', 'car', 'moo']
741
+ # a.grep(/ar/) # => ["bar", "car"]
742
+ # (1..10).grep(3..8) # => [3, 4, 5, 6, 7, 8]
743
+ # ['a', 'b', 0, 1].grep(Integer) # => [0, 1]
744
+ #
745
+ # With a block given, calls the block with each matching element and returns an
746
+ # array containing each object returned by the block:
747
+ #
748
+ # a = ['foo', 'bar', 'car', 'moo']
749
+ # a.grep(/ar/) {|element| element.upcase } # => ["BAR", "CAR"]
750
+ #
751
+ # Related: #grep_v.
752
+ #
753
+ def grep: (untyped arg0) -> ::Array[E]
754
+ | [U] (untyped arg0) { (E arg0) -> U } -> ::Array[U]
755
+
756
+ # <!--
757
+ # rdoc-file=enum.c
758
+ # - grep_v(pattern) -> array
759
+ # - grep_v(pattern) {|element| ... } -> array
760
+ # -->
761
+ # Returns an array of objects based on elements of `self` that <em>don't</em>
762
+ # match the given pattern.
763
+ #
764
+ # With no block given, returns an array containing each element for which
765
+ # <code>pattern === element</code> is `false`:
766
+ #
767
+ # a = ['foo', 'bar', 'car', 'moo']
768
+ # a.grep_v(/ar/) # => ["foo", "moo"]
769
+ # (1..10).grep_v(3..8) # => [1, 2, 9, 10]
770
+ # ['a', 'b', 0, 1].grep_v(Integer) # => ["a", "b"]
771
+ #
772
+ # With a block given, calls the block with each non-matching element and returns
773
+ # an array containing each object returned by the block:
774
+ #
775
+ # a = ['foo', 'bar', 'car', 'moo']
776
+ # a.grep_v(/ar/) {|element| element.upcase } # => ["FOO", "MOO"]
777
+ #
778
+ # Related: #grep.
779
+ #
780
+ def grep_v: (untyped) -> ::Array[E]
781
+ | [U] (untyped) { (E) -> U } -> ::Array[U]
782
+
783
+ # <!--
784
+ # rdoc-file=enum.c
785
+ # - group_by {|element| ... } -> hash
786
+ # - group_by -> enumerator
787
+ # -->
788
+ # With a block given returns a hash:
789
+ #
790
+ # * Each key is a return value from the block.
791
+ # * Each value is an array of those elements for which the block returned that
792
+ # key.
793
+ #
794
+ # Examples:
795
+ #
796
+ # g = (1..6).group_by {|i| i%3 }
797
+ # g # => {1=>[1, 4], 2=>[2, 5], 0=>[3, 6]}
798
+ # h = {foo: 0, bar: 1, baz: 0, bat: 1}
799
+ # g = h.group_by {|key, value| value }
800
+ # g # => {0=>[[:foo, 0], [:baz, 0]], 1=>[[:bar, 1], [:bat, 1]]}
801
+ #
802
+ # With no block given, returns an Enumerator.
803
+ #
804
+ def group_by: [U] () { (E arg0) -> U } -> ::Hash[U, ::Array[E]]
805
+ | () -> ::Enumerator[E, ::Array[E]]
806
+
807
+ # <!-- rdoc-file=enum.c -->
808
+ # Returns whether for any element <code>object == element</code>:
809
+ #
810
+ # (1..4).include?(2) # => true
811
+ # (1..4).include?(5) # => false
812
+ # (1..4).include?('2') # => false
813
+ # %w[a b c d].include?('b') # => true
814
+ # %w[a b c d].include?('2') # => false
815
+ # {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
816
+ # {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
817
+ # {foo: 0, bar: 1, baz: 2}.include?(0) # => false
818
+ #
819
+ def include?: (top arg0) -> bool
820
+
821
+ # <!--
822
+ # rdoc-file=enum.c
823
+ # - inject(symbol) -> object
824
+ # - inject(initial_value, symbol) -> object
825
+ # - inject {|memo, value| ... } -> object
826
+ # - inject(initial_value) {|memo, value| ... } -> object
827
+ # -->
828
+ # Returns the result of applying a reducer to an initial value and the first
829
+ # element of the Enumerable. It then takes the result and applies the function
830
+ # to it and the second element of the collection, and so on. The return value is
831
+ # the result returned by the final call to the function.
832
+ #
833
+ # You can think of
834
+ #
835
+ # [ a, b, c, d ].inject(i) { |r, v| fn(r, v) }
836
+ #
837
+ # as being
838
+ #
839
+ # fn(fn(fn(fn(i, a), b), c), d)
840
+ #
841
+ # In a way the `inject` function *injects* the function between the elements of
842
+ # the enumerable.
843
+ #
844
+ # `inject` is aliased as `reduce`. You use it when you want to *reduce* a
845
+ # collection to a single value.
846
+ #
847
+ # **The Calling Sequences**
848
+ #
849
+ # Let's start with the most verbose:
850
+ #
851
+ # enum.inject(initial_value) do |result, next_value|
852
+ # # do something with +result+ and +next_value+
853
+ # # the value returned by the block becomes the
854
+ # # value passed in to the next iteration
855
+ # # as +result+
856
+ # end
857
+ #
858
+ # For example:
859
+ #
860
+ # product = [ 2, 3, 4 ].inject(1) do |result, next_value|
861
+ # result * next_value
862
+ # end
863
+ # product #=> 24
864
+ #
865
+ # When this runs, the block is first called with `1` (the initial value) and `2`
866
+ # (the first element of the array). The block returns <code>1*2</code>, so on
867
+ # the next iteration the block is called with `2` (the previous result) and `3`.
868
+ # The block returns `6`, and is called one last time with `6` and `4`. The
869
+ # result of the block, `24` becomes the value returned by `inject`. This code
870
+ # returns the product of the elements in the enumerable.
871
+ #
872
+ # <strong>First Shortcut: Default Initial value</strong>
873
+ #
874
+ # In the case of the previous example, the initial value, `1`, wasn't really
875
+ # necessary: the calculation of the product of a list of numbers is
876
+ # self-contained.
877
+ #
878
+ # In these circumstances, you can omit the `initial_value` parameter. `inject`
879
+ # will then initially call the block with the first element of the collection as
880
+ # the `result` parameter and the second element as the `next_value`.
881
+ #
882
+ # [ 2, 3, 4 ].inject do |result, next_value|
883
+ # result * next_value
884
+ # end
885
+ #
886
+ # This shortcut is convenient, but can only be used when the block produces a
887
+ # result which can be passed back to it as a first parameter.
888
+ #
889
+ # Here's an example where that's not the case: it returns a hash where the keys
890
+ # are words and the values are the number of occurrences of that word in the
891
+ # enumerable.
892
+ #
893
+ # freqs = File.read("README.md")
894
+ # .scan(/\w{2,}/)
895
+ # .reduce(Hash.new(0)) do |counts, word|
896
+ # counts[word] += 1
897
+ # counts
898
+ # end
899
+ # freqs #=> {"Actions"=>4,
900
+ # "Status"=>5,
901
+ # "MinGW"=>3,
902
+ # "https"=>27,
903
+ # "github"=>10,
904
+ # "com"=>15, ...
905
+ #
906
+ # Note that the last line of the block is just the word `counts`. This ensures
907
+ # the return value of the block is the result that's being calculated.
908
+ #
909
+ # <strong>Second Shortcut: a Reducer function</strong>
910
+ #
911
+ # A *reducer function* is a function that takes a partial result and the next
912
+ # value, returning the next partial result. The block that is given to `inject`
913
+ # is a reducer.
914
+ #
915
+ # You can also write a reducer as a function and pass the name of that function
916
+ # (as a symbol) to `inject`. However, for this to work, the function
917
+ #
918
+ # 1. Must be defined on the type of the result value
919
+ # 2. Must accept a single parameter, the next value in the collection, and
920
+ # 3. Must return an updated result which will also implement the function.
921
+ #
922
+ # Here's an example that adds elements to a string. The two calls invoke the
923
+ # functions String#concat and String#+ on the result so far, passing it the next
924
+ # value.
925
+ #
926
+ # s = [ "cat", " ", "dog" ].inject("", :concat)
927
+ # s #=> "cat dog"
928
+ # s = [ "cat", " ", "dog" ].inject("The result is:", :+)
929
+ # s #=> "The result is: cat dog"
930
+ #
931
+ # Here's a more complex example when the result object maintains state of a
932
+ # different type to the enumerable elements.
933
+ #
934
+ # class Turtle
935
+ #
936
+ # def initialize
937
+ # @x = @y = 0
938
+ # end
939
+ #
940
+ # def move(dir)
941
+ # case dir
942
+ # when "n" then @y += 1
943
+ # when "s" then @y -= 1
944
+ # when "e" then @x += 1
945
+ # when "w" then @x -= 1
946
+ # end
947
+ # self
948
+ # end
949
+ # end
950
+ #
951
+ # position = "nnneesw".chars.reduce(Turtle.new, :move)
952
+ # position #=>> #<Turtle:0x00000001052f4698 @y=2, @x=1>
953
+ #
954
+ # <strong>Third Shortcut: Reducer With no Initial Value</strong>
955
+ #
956
+ # If your reducer returns a value that it can accept as a parameter, then you
957
+ # don't have to pass in an initial value. Here <code>:*</code> is the name of
958
+ # the *times* function:
959
+ #
960
+ # product = [ 2, 3, 4 ].inject(:*)
961
+ # product # => 24
962
+ #
963
+ # String concatenation again:
964
+ #
965
+ # s = [ "cat", " ", "dog" ].inject(:+)
966
+ # s #=> "cat dog"
967
+ #
968
+ # And an example that converts a hash to an array of two-element subarrays.
969
+ #
970
+ # nested = {foo: 0, bar: 1}.inject([], :push)
971
+ # nested # => [[:foo, 0], [:bar, 1]]
972
+ #
973
+ def inject: (untyped init, Symbol method) -> untyped
974
+ | (Symbol method) -> untyped
975
+ | [A] (A initial) { (A, E) -> A } -> A
976
+ | () { (E, E) -> E } -> E
977
+
978
+ # <!--
979
+ # rdoc-file=enum.c
980
+ # - max -> element
981
+ # - max(n) -> array
982
+ # - max {|a, b| ... } -> element
983
+ # - max(n) {|a, b| ... } -> array
984
+ # -->
985
+ # Returns the element with the maximum element according to a given criterion.
986
+ # The ordering of equal elements is indeterminate and may be unstable.
987
+ #
988
+ # With no argument and no block, returns the maximum element, using the
989
+ # elements' own method <code>#<=></code> for comparison:
990
+ #
991
+ # (1..4).max # => 4
992
+ # (-4..-1).max # => -1
993
+ # %w[d c b a].max # => "d"
994
+ # {foo: 0, bar: 1, baz: 2}.max # => [:foo, 0]
995
+ # [].max # => nil
996
+ #
997
+ # With positive integer argument `n` given, and no block, returns an array
998
+ # containing the first `n` maximum elements that exist:
999
+ #
1000
+ # (1..4).max(2) # => [4, 3]
1001
+ # (-4..-1).max(2) # => [-1, -2]
1002
+ # %w[d c b a].max(2) # => ["d", "c"]
1003
+ # {foo: 0, bar: 1, baz: 2}.max(2) # => [[:foo, 0], [:baz, 2]]
1004
+ # [].max(2) # => []
1005
+ #
1006
+ # With a block given, the block determines the maximum elements. The block is
1007
+ # called with two elements `a` and `b`, and must return:
1008
+ #
1009
+ # * A negative integer if <code>a < b</code>.
1010
+ # * Zero if <code>a == b</code>.
1011
+ # * A positive integer if <code>a > b</code>.
1012
+ #
1013
+ # With a block given and no argument, returns the maximum element as determined
1014
+ # by the block:
1015
+ #
1016
+ # %w[xxx x xxxx xx].max {|a, b| a.size <=> b.size } # => "xxxx"
1017
+ # h = {foo: 0, bar: 1, baz: 2}
1018
+ # h.max {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:baz, 2]
1019
+ # [].max {|a, b| a <=> b } # => nil
1020
+ #
1021
+ # With a block given and positive integer argument `n` given, returns an array
1022
+ # containing the first `n` maximum elements that exist, as determined by the
1023
+ # block.
1024
+ #
1025
+ # %w[xxx x xxxx xx].max(2) {|a, b| a.size <=> b.size } # => ["xxxx", "xxx"]
1026
+ # h = {foo: 0, bar: 1, baz: 2}
1027
+ # h.max(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
1028
+ # # => [[:baz, 2], [:bar, 1]]
1029
+ # [].max(2) {|a, b| a <=> b } # => []
1030
+ #
1031
+ # Related: #min, #minmax, #max_by.
1032
+ #
1033
+ def max: () -> E?
1034
+ | () { (E arg0, E arg1) -> Integer } -> E?
1035
+ | (Integer arg0) -> ::Array[E]
1036
+ | (Integer arg0) { (E arg0, E arg1) -> Integer } -> ::Array[E]
1037
+
1038
+ # <!--
1039
+ # rdoc-file=enum.c
1040
+ # - max_by {|element| ... } -> element
1041
+ # - max_by(n) {|element| ... } -> array
1042
+ # - max_by -> enumerator
1043
+ # - max_by(n) -> enumerator
1044
+ # -->
1045
+ # Returns the elements for which the block returns the maximum values.
1046
+ #
1047
+ # With a block given and no argument, returns the element for which the block
1048
+ # returns the maximum value:
1049
+ #
1050
+ # (1..4).max_by {|element| -element } # => 1
1051
+ # %w[a b c d].max_by {|element| -element.ord } # => "a"
1052
+ # {foo: 0, bar: 1, baz: 2}.max_by {|key, value| -value } # => [:foo, 0]
1053
+ # [].max_by {|element| -element } # => nil
1054
+ #
1055
+ # With a block given and positive integer argument `n` given, returns an array
1056
+ # containing the `n` elements for which the block returns maximum values:
1057
+ #
1058
+ # (1..4).max_by(2) {|element| -element }
1059
+ # # => [1, 2]
1060
+ # %w[a b c d].max_by(2) {|element| -element.ord }
1061
+ # # => ["a", "b"]
1062
+ # {foo: 0, bar: 1, baz: 2}.max_by(2) {|key, value| -value }
1063
+ # # => [[:foo, 0], [:bar, 1]]
1064
+ # [].max_by(2) {|element| -element }
1065
+ # # => []
1066
+ #
1067
+ # Returns an Enumerator if no block is given.
1068
+ #
1069
+ # Related: #max, #minmax, #min_by.
1070
+ #
1071
+ def max_by: () -> ::Enumerator[E, E?]
1072
+ | () { (E arg0) -> (Comparable | ::Array[untyped]) } -> E?
1073
+ | (Integer arg0) -> ::Enumerator[E, ::Array[E]]
1074
+ | (Integer arg0) { (E arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[E]
1075
+
1076
+ # <!--
1077
+ # rdoc-file=enum.c
1078
+ # - min -> element
1079
+ # - min(n) -> array
1080
+ # - min {|a, b| ... } -> element
1081
+ # - min(n) {|a, b| ... } -> array
1082
+ # -->
1083
+ # Returns the element with the minimum element according to a given criterion.
1084
+ # The ordering of equal elements is indeterminate and may be unstable.
1085
+ #
1086
+ # With no argument and no block, returns the minimum element, using the
1087
+ # elements' own method <code>#<=></code> for comparison:
1088
+ #
1089
+ # (1..4).min # => 1
1090
+ # (-4..-1).min # => -4
1091
+ # %w[d c b a].min # => "a"
1092
+ # {foo: 0, bar: 1, baz: 2}.min # => [:bar, 1]
1093
+ # [].min # => nil
1094
+ #
1095
+ # With positive integer argument `n` given, and no block, returns an array
1096
+ # containing the first `n` minimum elements that exist:
1097
+ #
1098
+ # (1..4).min(2) # => [1, 2]
1099
+ # (-4..-1).min(2) # => [-4, -3]
1100
+ # %w[d c b a].min(2) # => ["a", "b"]
1101
+ # {foo: 0, bar: 1, baz: 2}.min(2) # => [[:bar, 1], [:baz, 2]]
1102
+ # [].min(2) # => []
1103
+ #
1104
+ # With a block given, the block determines the minimum elements. The block is
1105
+ # called with two elements `a` and `b`, and must return:
1106
+ #
1107
+ # * A negative integer if <code>a < b</code>.
1108
+ # * Zero if <code>a == b</code>.
1109
+ # * A positive integer if <code>a > b</code>.
1110
+ #
1111
+ # With a block given and no argument, returns the minimum element as determined
1112
+ # by the block:
1113
+ #
1114
+ # %w[xxx x xxxx xx].min {|a, b| a.size <=> b.size } # => "x"
1115
+ # h = {foo: 0, bar: 1, baz: 2}
1116
+ # h.min {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:foo, 0]
1117
+ # [].min {|a, b| a <=> b } # => nil
1118
+ #
1119
+ # With a block given and positive integer argument `n` given, returns an array
1120
+ # containing the first `n` minimum elements that exist, as determined by the
1121
+ # block.
1122
+ #
1123
+ # %w[xxx x xxxx xx].min(2) {|a, b| a.size <=> b.size } # => ["x", "xx"]
1124
+ # h = {foo: 0, bar: 1, baz: 2}
1125
+ # h.min(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
1126
+ # # => [[:foo, 0], [:bar, 1]]
1127
+ # [].min(2) {|a, b| a <=> b } # => []
1128
+ #
1129
+ # Related: #min_by, #minmax, #max.
1130
+ #
1131
+ def min: () -> E?
1132
+ | () { (E arg0, E arg1) -> Integer } -> E?
1133
+ | (Integer arg0) -> ::Array[E]
1134
+ | (Integer arg0) { (E arg0, E arg1) -> Integer } -> ::Array[E]
1135
+
1136
+ # <!--
1137
+ # rdoc-file=enum.c
1138
+ # - min_by {|element| ... } -> element
1139
+ # - min_by(n) {|element| ... } -> array
1140
+ # - min_by -> enumerator
1141
+ # - min_by(n) -> enumerator
1142
+ # -->
1143
+ # Returns the elements for which the block returns the minimum values.
1144
+ #
1145
+ # With a block given and no argument, returns the element for which the block
1146
+ # returns the minimum value:
1147
+ #
1148
+ # (1..4).min_by {|element| -element } # => 4
1149
+ # %w[a b c d].min_by {|element| -element.ord } # => "d"
1150
+ # {foo: 0, bar: 1, baz: 2}.min_by {|key, value| -value } # => [:baz, 2]
1151
+ # [].min_by {|element| -element } # => nil
1152
+ #
1153
+ # With a block given and positive integer argument `n` given, returns an array
1154
+ # containing the `n` elements for which the block returns minimum values:
1155
+ #
1156
+ # (1..4).min_by(2) {|element| -element }
1157
+ # # => [4, 3]
1158
+ # %w[a b c d].min_by(2) {|element| -element.ord }
1159
+ # # => ["d", "c"]
1160
+ # {foo: 0, bar: 1, baz: 2}.min_by(2) {|key, value| -value }
1161
+ # # => [[:baz, 2], [:bar, 1]]
1162
+ # [].min_by(2) {|element| -element }
1163
+ # # => []
1164
+ #
1165
+ # Returns an Enumerator if no block is given.
1166
+ #
1167
+ # Related: #min, #minmax, #max_by.
1168
+ #
1169
+ def min_by: () -> ::Enumerator[E, E?]
1170
+ | () { (E arg0) -> (Comparable | ::Array[untyped]) } -> E?
1171
+ | (Integer arg0) -> ::Enumerator[E, ::Array[E]]
1172
+ | (Integer arg0) { (E arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[E]
1173
+
1174
+ # <!--
1175
+ # rdoc-file=enum.c
1176
+ # - minmax -> [minimum, maximum]
1177
+ # - minmax {|a, b| ... } -> [minimum, maximum]
1178
+ # -->
1179
+ # Returns a 2-element array containing the minimum and maximum elements
1180
+ # according to a given criterion. The ordering of equal elements is
1181
+ # indeterminate and may be unstable.
1182
+ #
1183
+ # With no argument and no block, returns the minimum and maximum elements, using
1184
+ # the elements' own method <code>#<=></code> for comparison:
1185
+ #
1186
+ # (1..4).minmax # => [1, 4]
1187
+ # (-4..-1).minmax # => [-4, -1]
1188
+ # %w[d c b a].minmax # => ["a", "d"]
1189
+ # {foo: 0, bar: 1, baz: 2}.minmax # => [[:bar, 1], [:foo, 0]]
1190
+ # [].minmax # => [nil, nil]
1191
+ #
1192
+ # With a block given, returns the minimum and maximum elements as determined by
1193
+ # the block:
1194
+ #
1195
+ # %w[xxx x xxxx xx].minmax {|a, b| a.size <=> b.size } # => ["x", "xxxx"]
1196
+ # h = {foo: 0, bar: 1, baz: 2}
1197
+ # h.minmax {|pair1, pair2| pair1[1] <=> pair2[1] }
1198
+ # # => [[:foo, 0], [:baz, 2]]
1199
+ # [].minmax {|a, b| a <=> b } # => [nil, nil]
1200
+ #
1201
+ # Related: #min, #max, #minmax_by.
1202
+ #
1203
+ def minmax: () -> [ E?, E? ]
1204
+ | () { (E arg0, E arg1) -> Integer } -> [ E?, E? ]
1205
+
1206
+ # <!--
1207
+ # rdoc-file=enum.c
1208
+ # - minmax_by {|element| ... } -> [minimum, maximum]
1209
+ # - minmax_by -> enumerator
1210
+ # -->
1211
+ # Returns a 2-element array containing the elements for which the block returns
1212
+ # minimum and maximum values:
1213
+ #
1214
+ # (1..4).minmax_by {|element| -element }
1215
+ # # => [4, 1]
1216
+ # %w[a b c d].minmax_by {|element| -element.ord }
1217
+ # # => ["d", "a"]
1218
+ # {foo: 0, bar: 1, baz: 2}.minmax_by {|key, value| -value }
1219
+ # # => [[:baz, 2], [:foo, 0]]
1220
+ # [].minmax_by {|element| -element }
1221
+ # # => [nil, nil]
1222
+ #
1223
+ # Returns an Enumerator if no block is given.
1224
+ #
1225
+ # Related: #max_by, #minmax, #min_by.
1226
+ #
1227
+ def minmax_by: () -> [ E?, E? ]
1228
+ | () { (E arg0) -> (Comparable | ::Array[untyped]) } -> [ E?, E? ]
1229
+
1230
+ # <!--
1231
+ # rdoc-file=enum.c
1232
+ # - none? -> true or false
1233
+ # - none?(pattern) -> true or false
1234
+ # - none? {|element| ... } -> true or false
1235
+ # -->
1236
+ # Returns whether no element meets a given criterion.
1237
+ #
1238
+ # With no argument and no block, returns whether no element is truthy:
1239
+ #
1240
+ # (1..4).none? # => false
1241
+ # [nil, false].none? # => true
1242
+ # {foo: 0}.none? # => false
1243
+ # {foo: 0, bar: 1}.none? # => false
1244
+ # [].none? # => true
1245
+ #
1246
+ # With argument `pattern` and no block, returns whether for no element
1247
+ # `element`, <code>pattern === element</code>:
1248
+ #
1249
+ # [nil, false, 1.1].none?(Integer) # => true
1250
+ # %w[bar baz bat bam].none?(/m/) # => false
1251
+ # %w[bar baz bat bam].none?(/foo/) # => true
1252
+ # %w[bar baz bat bam].none?('ba') # => true
1253
+ # {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true
1254
+ # {foo: 0}.none?(Array) # => false
1255
+ # [].none?(Integer) # => true
1256
+ #
1257
+ # With a block given, returns whether the block returns a truthy value for no
1258
+ # element:
1259
+ #
1260
+ # (1..4).none? {|element| element < 1 } # => true
1261
+ # (1..4).none? {|element| element < 2 } # => false
1262
+ # {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true
1263
+ # {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
1264
+ #
1265
+ # Related: #one?, #all?, #any?.
1266
+ #
1267
+ def none?: () -> bool
1268
+ | (_Pattern) -> bool
1269
+ | () { (E) -> boolish } -> bool
1270
+
1271
+ # <!--
1272
+ # rdoc-file=enum.c
1273
+ # - one? -> true or false
1274
+ # - one?(pattern) -> true or false
1275
+ # - one? {|element| ... } -> true or false
1276
+ # -->
1277
+ # Returns whether exactly one element meets a given criterion.
1278
+ #
1279
+ # With no argument and no block, returns whether exactly one element is truthy:
1280
+ #
1281
+ # (1..1).one? # => true
1282
+ # [1, nil, false].one? # => true
1283
+ # (1..4).one? # => false
1284
+ # {foo: 0}.one? # => true
1285
+ # {foo: 0, bar: 1}.one? # => false
1286
+ # [].one? # => false
1287
+ #
1288
+ # With argument `pattern` and no block, returns whether for exactly one element
1289
+ # `element`, <code>pattern === element</code>:
1290
+ #
1291
+ # [nil, false, 0].one?(Integer) # => true
1292
+ # [nil, false, 0].one?(Numeric) # => true
1293
+ # [nil, false, 0].one?(Float) # => false
1294
+ # %w[bar baz bat bam].one?(/m/) # => true
1295
+ # %w[bar baz bat bam].one?(/foo/) # => false
1296
+ # %w[bar baz bat bam].one?('ba') # => false
1297
+ # {foo: 0, bar: 1, baz: 2}.one?(Array) # => false
1298
+ # {foo: 0}.one?(Array) # => true
1299
+ # [].one?(Integer) # => false
1300
+ #
1301
+ # With a block given, returns whether the block returns a truthy value for
1302
+ # exactly one element:
1303
+ #
1304
+ # (1..4).one? {|element| element < 2 } # => true
1305
+ # (1..4).one? {|element| element < 1 } # => false
1306
+ # {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true
1307
+ # {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
1308
+ #
1309
+ # Related: #none?, #all?, #any?.
1310
+ #
1311
+ def one?: () -> bool
1312
+ | (_Pattern) -> bool
1313
+ | () { (E) -> boolish } -> bool
1314
+
1315
+ # <!--
1316
+ # rdoc-file=enum.c
1317
+ # - partition {|element| ... } -> [true_array, false_array]
1318
+ # - partition -> enumerator
1319
+ # -->
1320
+ # With a block given, returns an array of two arrays:
1321
+ #
1322
+ # * The first having those elements for which the block returns a truthy
1323
+ # value.
1324
+ # * The other having all other elements.
1325
+ #
1326
+ # Examples:
1327
+ #
1328
+ # p = (1..4).partition {|i| i.even? }
1329
+ # p # => [[2, 4], [1, 3]]
1330
+ # p = ('a'..'d').partition {|c| c < 'c' }
1331
+ # p # => [["a", "b"], ["c", "d"]]
1332
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
1333
+ # p = h.partition {|key, value| key.start_with?('b') }
1334
+ # p # => [[[:bar, 1], [:baz, 2], [:bat, 3]], [[:foo, 0]]]
1335
+ # p = h.partition {|key, value| value < 2 }
1336
+ # p # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]]]
1337
+ #
1338
+ # With no block given, returns an Enumerator.
1339
+ #
1340
+ # Related: Enumerable#group_by.
1341
+ #
1342
+ def partition: () { (E) -> boolish } -> [ ::Array[E], ::Array[E] ]
1343
+ | () -> ::Enumerator[E, [ ::Array[E], ::Array[E] ]]
1344
+
1345
+ # <!--
1346
+ # rdoc-file=enum.c
1347
+ # - reject {|element| ... } -> array
1348
+ # - reject -> enumerator
1349
+ # -->
1350
+ # Returns an array of objects rejected by the block.
1351
+ #
1352
+ # With a block given, calls the block with successive elements; returns an array
1353
+ # of those elements for which the block returns `nil` or `false`:
1354
+ #
1355
+ # (0..9).reject {|i| i * 2 if i.even? } # => [1, 3, 5, 7, 9]
1356
+ # {foo: 0, bar: 1, baz: 2}.reject {|key, value| key if value.odd? } # => {:foo=>0, :baz=>2}
1357
+ #
1358
+ # When no block given, returns an Enumerator.
1359
+ #
1360
+ # Related: #select.
1361
+ #
1362
+ def reject: () { (E) -> boolish } -> ::Array[E]
1363
+ | () -> ::Enumerator[E, ::Array[E]]
1364
+
1365
+ # <!--
1366
+ # rdoc-file=enum.c
1367
+ # - reverse_each(*args) {|element| ... } -> self
1368
+ # - reverse_each(*args) -> enumerator
1369
+ # -->
1370
+ # With a block given, calls the block with each element, but in reverse order;
1371
+ # returns `self`:
1372
+ #
1373
+ # a = []
1374
+ # (1..4).reverse_each {|element| a.push(-element) } # => 1..4
1375
+ # a # => [-4, -3, -2, -1]
1376
+ #
1377
+ # a = []
1378
+ # %w[a b c d].reverse_each {|element| a.push(element) }
1379
+ # # => ["a", "b", "c", "d"]
1380
+ # a # => ["d", "c", "b", "a"]
1381
+ #
1382
+ # a = []
1383
+ # h.reverse_each {|element| a.push(element) }
1384
+ # # => {:foo=>0, :bar=>1, :baz=>2}
1385
+ # a # => [[:baz, 2], [:bar, 1], [:foo, 0]]
1386
+ #
1387
+ # With no block given, returns an Enumerator.
1388
+ #
1389
+ def reverse_each: () { (E arg0) -> untyped } -> void
1390
+ | () -> ::Enumerator[E]
1391
+
1392
+ # <!--
1393
+ # rdoc-file=enum.c
1394
+ # - sort -> array
1395
+ # - sort {|a, b| ... } -> array
1396
+ # -->
1397
+ # Returns an array containing the sorted elements of `self`. The ordering of
1398
+ # equal elements is indeterminate and may be unstable.
1399
+ #
1400
+ # With no block given, the sort compares using the elements' own method
1401
+ # <code>#<=></code>:
1402
+ #
1403
+ # %w[b c a d].sort # => ["a", "b", "c", "d"]
1404
+ # {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
1405
+ #
1406
+ # With a block given, comparisons in the block determine the ordering. The block
1407
+ # is called with two elements `a` and `b`, and must return:
1408
+ #
1409
+ # * A negative integer if <code>a < b</code>.
1410
+ # * Zero if <code>a == b</code>.
1411
+ # * A positive integer if <code>a > b</code>.
1412
+ #
1413
+ # Examples:
1414
+ #
1415
+ # a = %w[b c a d]
1416
+ # a.sort {|a, b| b <=> a } # => ["d", "c", "b", "a"]
1417
+ # h = {foo: 0, bar: 1, baz: 2}
1418
+ # h.sort {|a, b| b <=> a } # => [[:foo, 0], [:baz, 2], [:bar, 1]]
1419
+ #
1420
+ # See also #sort_by. It implements a Schwartzian transform which is useful when
1421
+ # key computation or comparison is expensive.
1422
+ #
1423
+ def sort: () -> ::Array[E]
1424
+ | () { (E arg0, E arg1) -> Integer } -> ::Array[E]
1425
+
1426
+ # <!--
1427
+ # rdoc-file=enum.c
1428
+ # - sort_by {|element| ... } -> array
1429
+ # - sort_by -> enumerator
1430
+ # -->
1431
+ # With a block given, returns an array of elements of `self`, sorted according
1432
+ # to the value returned by the block for each element. The ordering of equal
1433
+ # elements is indeterminate and may be unstable.
1434
+ #
1435
+ # Examples:
1436
+ #
1437
+ # a = %w[xx xxx x xxxx]
1438
+ # a.sort_by {|s| s.size } # => ["x", "xx", "xxx", "xxxx"]
1439
+ # a.sort_by {|s| -s.size } # => ["xxxx", "xxx", "xx", "x"]
1440
+ # h = {foo: 2, bar: 1, baz: 0}
1441
+ # h.sort_by{|key, value| value } # => [[:baz, 0], [:bar, 1], [:foo, 2]]
1442
+ # h.sort_by{|key, value| key } # => [[:bar, 1], [:baz, 0], [:foo, 2]]
1443
+ #
1444
+ # With no block given, returns an Enumerator.
1445
+ #
1446
+ # The current implementation of #sort_by generates an array of tuples containing
1447
+ # the original collection element and the mapped value. This makes #sort_by
1448
+ # fairly expensive when the keysets are simple.
1449
+ #
1450
+ # require 'benchmark'
1451
+ #
1452
+ # a = (1..100000).map { rand(100000) }
1453
+ #
1454
+ # Benchmark.bm(10) do |b|
1455
+ # b.report("Sort") { a.sort }
1456
+ # b.report("Sort by") { a.sort_by { |a| a } }
1457
+ # end
1458
+ #
1459
+ # <em>produces:</em>
1460
+ #
1461
+ # user system total real
1462
+ # Sort 0.180000 0.000000 0.180000 ( 0.175469)
1463
+ # Sort by 1.980000 0.040000 2.020000 ( 2.013586)
1464
+ #
1465
+ # However, consider the case where comparing the keys is a non-trivial
1466
+ # operation. The following code sorts some files on modification time using the
1467
+ # basic #sort method.
1468
+ #
1469
+ # files = Dir["*"]
1470
+ # sorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime }
1471
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1472
+ #
1473
+ # This sort is inefficient: it generates two new File objects during every
1474
+ # comparison. A slightly better technique is to use the Kernel#test method to
1475
+ # generate the modification times directly.
1476
+ #
1477
+ # files = Dir["*"]
1478
+ # sorted = files.sort { |a, b|
1479
+ # test(?M, a) <=> test(?M, b)
1480
+ # }
1481
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1482
+ #
1483
+ # This still generates many unnecessary Time objects. A more efficient technique
1484
+ # is to cache the sort keys (modification times in this case) before the sort.
1485
+ # Perl users often call this approach a Schwartzian transform, after Randal
1486
+ # Schwartz. We construct a temporary array, where each element is an array
1487
+ # containing our sort key along with the filename. We sort this array, and then
1488
+ # extract the filename from the result.
1489
+ #
1490
+ # sorted = Dir["*"].collect { |f|
1491
+ # [test(?M, f), f]
1492
+ # }.sort.collect { |f| f[1] }
1493
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1494
+ #
1495
+ # This is exactly what #sort_by does internally.
1496
+ #
1497
+ # sorted = Dir["*"].sort_by { |f| test(?M, f) }
1498
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1499
+ #
1500
+ # To produce the reverse of a specific order, the following can be used:
1501
+ #
1502
+ # ary.sort_by { ... }.reverse!
1503
+ #
1504
+ def sort_by: () { (E arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[E]
1505
+ | () -> ::Enumerator[E, ::Array[E]]
1506
+
1507
+ # <!--
1508
+ # rdoc-file=enum.c
1509
+ # - take(n) -> array
1510
+ # -->
1511
+ # For non-negative integer `n`, returns the first `n` elements:
1512
+ #
1513
+ # r = (1..4)
1514
+ # r.take(2) # => [1, 2]
1515
+ # r.take(0) # => []
1516
+ #
1517
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
1518
+ # h.take(2) # => [[:foo, 0], [:bar, 1]]
1519
+ #
1520
+ def take: (Integer n) -> ::Array[E]
1521
+
1522
+ # <!--
1523
+ # rdoc-file=enum.c
1524
+ # - take_while {|element| ... } -> array
1525
+ # - take_while -> enumerator
1526
+ # -->
1527
+ # Calls the block with successive elements as long as the block returns a truthy
1528
+ # value; returns an array of all elements up to that point:
1529
+ #
1530
+ # (1..4).take_while{|i| i < 3 } # => [1, 2]
1531
+ # h = {foo: 0, bar: 1, baz: 2}
1532
+ # h.take_while{|element| key, value = *element; value < 2 }
1533
+ # # => [[:foo, 0], [:bar, 1]]
1534
+ #
1535
+ # With no block given, returns an Enumerator.
1536
+ #
1537
+ def take_while: () { (E) -> boolish } -> ::Array[E]
1538
+ | () -> ::Enumerator[E, ::Array[E]]
1539
+
1540
+ # <!--
1541
+ # rdoc-file=enum.c
1542
+ # - to_h(*args) -> hash
1543
+ # - to_h(*args) {|element| ... } -> hash
1544
+ # -->
1545
+ # When `self` consists of 2-element arrays, returns a hash each of whose entries
1546
+ # is the key-value pair formed from one of those arrays:
1547
+ #
1548
+ # [[:foo, 0], [:bar, 1], [:baz, 2]].to_h # => {:foo=>0, :bar=>1, :baz=>2}
1549
+ #
1550
+ # When a block is given, the block is called with each element of `self`; the
1551
+ # block should return a 2-element array which becomes a key-value pair in the
1552
+ # returned hash:
1553
+ #
1554
+ # (0..3).to_h {|i| [i, i ** 2]} # => {0=>0, 1=>1, 2=>4, 3=>9}
1555
+ #
1556
+ # Raises an exception if an element of `self` is not a 2-element array, and a
1557
+ # block is not passed.
1558
+ #
1559
+ def to_h: () -> ::Hash[untyped, untyped]
1560
+ | [T, U] () { (E) -> [ T, U ] } -> ::Hash[T, U]
1561
+
1562
+ # <!--
1563
+ # rdoc-file=enum.c
1564
+ # - each_slice(n) { ... } -> self
1565
+ # - each_slice(n) -> enumerator
1566
+ # -->
1567
+ # Calls the block with each successive disjoint `n`-tuple of elements; returns
1568
+ # `self`:
1569
+ #
1570
+ # a = []
1571
+ # (1..10).each_slice(3) {|tuple| a.push(tuple) }
1572
+ # a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
1573
+ #
1574
+ # a = []
1575
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
1576
+ # h.each_slice(2) {|tuple| a.push(tuple) }
1577
+ # a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
1578
+ #
1579
+ # With no block given, returns an Enumerator.
1580
+ #
1581
+ def each_slice: (Integer n) { (::Array[E]) -> void } -> self
1582
+ | (Integer n) -> ::Enumerator[::Array[E], self]
1583
+
1584
+ interface _NotFound[T]
1585
+ def call: () -> T
1586
+ end
1587
+
1588
+ # <!--
1589
+ # rdoc-file=enum.c
1590
+ # - find(if_none_proc = nil) {|element| ... } -> object or nil
1591
+ # - find(if_none_proc = nil) -> enumerator
1592
+ # -->
1593
+ # Returns the first element for which the block returns a truthy value.
1594
+ #
1595
+ # With a block given, calls the block with successive elements of the
1596
+ # collection; returns the first element for which the block returns a truthy
1597
+ # value:
1598
+ #
1599
+ # (0..9).find {|element| element > 2} # => 3
1600
+ #
1601
+ # If no such element is found, calls `if_none_proc` and returns its return
1602
+ # value.
1603
+ #
1604
+ # (0..9).find(proc {false}) {|element| element > 12} # => false
1605
+ # {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
1606
+ # {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
1607
+ #
1608
+ # With no block given, returns an Enumerator.
1609
+ #
1610
+ def find: () { (E) -> boolish } -> E?
1611
+ | () -> ::Enumerator[E, E?]
1612
+ | [T] (_NotFound[T] ifnone) { (E) -> boolish } -> (E | T)
1613
+ | [T] (_NotFound[T] ifnone) -> ::Enumerator[E, E | T]
1614
+
1615
+ # <!--
1616
+ # rdoc-file=enum.c
1617
+ # - flat_map {|element| ... } -> array
1618
+ # - flat_map -> enumerator
1619
+ # -->
1620
+ # Returns an array of flattened objects returned by the block.
1621
+ #
1622
+ # With a block given, calls the block with successive elements; returns a
1623
+ # flattened array of objects returned by the block:
1624
+ #
1625
+ # [0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3]
1626
+ # [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3]
1627
+ # [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
1628
+ # {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
1629
+ #
1630
+ # With no block given, returns an Enumerator.
1631
+ #
1632
+ # Alias: #collect_concat.
1633
+ #
1634
+ def flat_map: [U] () { (E) -> (Array[U] | U) } -> Array[U]
1635
+ | () -> ::Enumerator[E, Array[untyped]]
1636
+
1637
+ # <!-- rdoc-file=enum.c -->
1638
+ # Returns an array of objects returned by the block.
1639
+ #
1640
+ # With a block given, calls the block with successive elements; returns an array
1641
+ # of the objects returned by the block:
1642
+ #
1643
+ # (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
1644
+ # {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
1645
+ #
1646
+ # With no block given, returns an Enumerator.
1647
+ #
1648
+ def map: [U] () { (E arg0) -> U } -> ::Array[U]
1649
+ | () -> ::Enumerator[E, ::Array[untyped]]
1650
+
1651
+ # <!--
1652
+ # rdoc-file=enum.c
1653
+ # - include?(object) -> true or false
1654
+ # -->
1655
+ # Returns whether for any element <code>object == element</code>:
1656
+ #
1657
+ # (1..4).include?(2) # => true
1658
+ # (1..4).include?(5) # => false
1659
+ # (1..4).include?('2') # => false
1660
+ # %w[a b c d].include?('b') # => true
1661
+ # %w[a b c d].include?('2') # => false
1662
+ # {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
1663
+ # {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
1664
+ # {foo: 0, bar: 1, baz: 2}.include?(0) # => false
1665
+ #
1666
+ def member?: (top arg0) -> bool
1667
+
1668
+ # <!-- rdoc-file=enum.c -->
1669
+ # Returns the result of applying a reducer to an initial value and the first
1670
+ # element of the Enumerable. It then takes the result and applies the function
1671
+ # to it and the second element of the collection, and so on. The return value is
1672
+ # the result returned by the final call to the function.
1673
+ #
1674
+ # You can think of
1675
+ #
1676
+ # [ a, b, c, d ].inject(i) { |r, v| fn(r, v) }
1677
+ #
1678
+ # as being
1679
+ #
1680
+ # fn(fn(fn(fn(i, a), b), c), d)
1681
+ #
1682
+ # In a way the `inject` function *injects* the function between the elements of
1683
+ # the enumerable.
1684
+ #
1685
+ # `inject` is aliased as `reduce`. You use it when you want to *reduce* a
1686
+ # collection to a single value.
1687
+ #
1688
+ # **The Calling Sequences**
1689
+ #
1690
+ # Let's start with the most verbose:
1691
+ #
1692
+ # enum.inject(initial_value) do |result, next_value|
1693
+ # # do something with +result+ and +next_value+
1694
+ # # the value returned by the block becomes the
1695
+ # # value passed in to the next iteration
1696
+ # # as +result+
1697
+ # end
1698
+ #
1699
+ # For example:
1700
+ #
1701
+ # product = [ 2, 3, 4 ].inject(1) do |result, next_value|
1702
+ # result * next_value
1703
+ # end
1704
+ # product #=> 24
1705
+ #
1706
+ # When this runs, the block is first called with `1` (the initial value) and `2`
1707
+ # (the first element of the array). The block returns <code>1*2</code>, so on
1708
+ # the next iteration the block is called with `2` (the previous result) and `3`.
1709
+ # The block returns `6`, and is called one last time with `6` and `4`. The
1710
+ # result of the block, `24` becomes the value returned by `inject`. This code
1711
+ # returns the product of the elements in the enumerable.
1712
+ #
1713
+ # <strong>First Shortcut: Default Initial value</strong>
1714
+ #
1715
+ # In the case of the previous example, the initial value, `1`, wasn't really
1716
+ # necessary: the calculation of the product of a list of numbers is
1717
+ # self-contained.
1718
+ #
1719
+ # In these circumstances, you can omit the `initial_value` parameter. `inject`
1720
+ # will then initially call the block with the first element of the collection as
1721
+ # the `result` parameter and the second element as the `next_value`.
1722
+ #
1723
+ # [ 2, 3, 4 ].inject do |result, next_value|
1724
+ # result * next_value
1725
+ # end
1726
+ #
1727
+ # This shortcut is convenient, but can only be used when the block produces a
1728
+ # result which can be passed back to it as a first parameter.
1729
+ #
1730
+ # Here's an example where that's not the case: it returns a hash where the keys
1731
+ # are words and the values are the number of occurrences of that word in the
1732
+ # enumerable.
1733
+ #
1734
+ # freqs = File.read("README.md")
1735
+ # .scan(/\w{2,}/)
1736
+ # .reduce(Hash.new(0)) do |counts, word|
1737
+ # counts[word] += 1
1738
+ # counts
1739
+ # end
1740
+ # freqs #=> {"Actions"=>4,
1741
+ # "Status"=>5,
1742
+ # "MinGW"=>3,
1743
+ # "https"=>27,
1744
+ # "github"=>10,
1745
+ # "com"=>15, ...
1746
+ #
1747
+ # Note that the last line of the block is just the word `counts`. This ensures
1748
+ # the return value of the block is the result that's being calculated.
1749
+ #
1750
+ # <strong>Second Shortcut: a Reducer function</strong>
1751
+ #
1752
+ # A *reducer function* is a function that takes a partial result and the next
1753
+ # value, returning the next partial result. The block that is given to `inject`
1754
+ # is a reducer.
1755
+ #
1756
+ # You can also write a reducer as a function and pass the name of that function
1757
+ # (as a symbol) to `inject`. However, for this to work, the function
1758
+ #
1759
+ # 1. Must be defined on the type of the result value
1760
+ # 2. Must accept a single parameter, the next value in the collection, and
1761
+ # 3. Must return an updated result which will also implement the function.
1762
+ #
1763
+ # Here's an example that adds elements to a string. The two calls invoke the
1764
+ # functions String#concat and String#+ on the result so far, passing it the next
1765
+ # value.
1766
+ #
1767
+ # s = [ "cat", " ", "dog" ].inject("", :concat)
1768
+ # s #=> "cat dog"
1769
+ # s = [ "cat", " ", "dog" ].inject("The result is:", :+)
1770
+ # s #=> "The result is: cat dog"
1771
+ #
1772
+ # Here's a more complex example when the result object maintains state of a
1773
+ # different type to the enumerable elements.
1774
+ #
1775
+ # class Turtle
1776
+ #
1777
+ # def initialize
1778
+ # @x = @y = 0
1779
+ # end
1780
+ #
1781
+ # def move(dir)
1782
+ # case dir
1783
+ # when "n" then @y += 1
1784
+ # when "s" then @y -= 1
1785
+ # when "e" then @x += 1
1786
+ # when "w" then @x -= 1
1787
+ # end
1788
+ # self
1789
+ # end
1790
+ # end
1791
+ #
1792
+ # position = "nnneesw".chars.reduce(Turtle.new, :move)
1793
+ # position #=>> #<Turtle:0x00000001052f4698 @y=2, @x=1>
1794
+ #
1795
+ # <strong>Third Shortcut: Reducer With no Initial Value</strong>
1796
+ #
1797
+ # If your reducer returns a value that it can accept as a parameter, then you
1798
+ # don't have to pass in an initial value. Here <code>:*</code> is the name of
1799
+ # the *times* function:
1800
+ #
1801
+ # product = [ 2, 3, 4 ].inject(:*)
1802
+ # product # => 24
1803
+ #
1804
+ # String concatenation again:
1805
+ #
1806
+ # s = [ "cat", " ", "dog" ].inject(:+)
1807
+ # s #=> "cat dog"
1808
+ #
1809
+ # And an example that converts a hash to an array of two-element subarrays.
1810
+ #
1811
+ # nested = {foo: 0, bar: 1}.inject([], :push)
1812
+ # nested # => [[:foo, 0], [:bar, 1]]
1813
+ #
1814
+ alias reduce inject
1815
+
1816
+ # <!--
1817
+ # rdoc-file=enum.c
1818
+ # - to_a(*args) -> array
1819
+ # -->
1820
+ # Returns an array containing the items in `self`:
1821
+ #
1822
+ # (0..4).to_a # => [0, 1, 2, 3, 4]
1823
+ #
1824
+ def to_a: () -> ::Array[E]
1825
+
1826
+ # <!--
1827
+ # rdoc-file=enumerator.c
1828
+ # - e.lazy -> lazy_enumerator
1829
+ # -->
1830
+ # Returns an Enumerator::Lazy, which redefines most Enumerable methods to
1831
+ # postpone enumeration and enumerate values only on an as-needed basis.
1832
+ #
1833
+ # ### Example
1834
+ #
1835
+ # The following program finds pythagorean triples:
1836
+ #
1837
+ # def pythagorean_triples
1838
+ # (1..Float::INFINITY).lazy.flat_map {|z|
1839
+ # (1..z).flat_map {|x|
1840
+ # (x..z).select {|y|
1841
+ # x**2 + y**2 == z**2
1842
+ # }.map {|y|
1843
+ # [x, y, z]
1844
+ # }
1845
+ # }
1846
+ # }
1847
+ # end
1848
+ # # show first ten pythagorean triples
1849
+ # p pythagorean_triples.take(10).force # take is lazy, so force is needed
1850
+ # p pythagorean_triples.first(10) # first is eager
1851
+ # # show pythagorean triples less than 100
1852
+ # p pythagorean_triples.take_while { |*, z| z < 100 }.force
1853
+ #
1854
+ def lazy: () -> Enumerator::Lazy[E]
1855
+
1856
+ # <!--
1857
+ # rdoc-file=enum.c
1858
+ # - uniq -> array
1859
+ # - uniq {|element| ... } -> array
1860
+ # -->
1861
+ # With no block, returns a new array containing only unique elements; the array
1862
+ # has no two elements `e0` and `e1` such that <code>e0.eql?(e1)</code>:
1863
+ #
1864
+ # %w[a b c c b a a b c].uniq # => ["a", "b", "c"]
1865
+ # [0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]
1866
+ #
1867
+ # With a block, returns a new array containing elements only for which the block
1868
+ # returns a unique value:
1869
+ #
1870
+ # a = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
1871
+ # a.uniq {|i| i.even? ? i : 0 } # => [0, 2, 4]
1872
+ # a = %w[a b c d e e d c b a a b c d e]
1873
+ # a.uniq {|c| c < 'c' } # => ["a", "c"]
1874
+ #
1875
+ def uniq: () -> ::Array[E]
1876
+ | () { (E item) -> untyped } -> ::Array[E]
1877
+
1878
+ # <!--
1879
+ # rdoc-file=enum.c
1880
+ # - sum(initial_value = 0) -> number
1881
+ # - sum(initial_value = 0) {|element| ... } -> object
1882
+ # -->
1883
+ # With no block given, returns the sum of `initial_value` and the elements:
1884
+ #
1885
+ # (1..100).sum # => 5050
1886
+ # (1..100).sum(1) # => 5051
1887
+ # ('a'..'d').sum('foo') # => "fooabcd"
1888
+ #
1889
+ # Generally, the sum is computed using methods <code>+</code> and `each`; for
1890
+ # performance optimizations, those methods may not be used, and so any
1891
+ # redefinition of those methods may not have effect here.
1892
+ #
1893
+ # One such optimization: When possible, computes using Gauss's summation formula
1894
+ # <em>n(n+1)/2</em>:
1895
+ #
1896
+ # 100 * (100 + 1) / 2 # => 5050
1897
+ #
1898
+ # With a block given, calls the block with each element; returns the sum of
1899
+ # `initial_value` and the block return values:
1900
+ #
1901
+ # (1..4).sum {|i| i*i } # => 30
1902
+ # (1..4).sum(100) {|i| i*i } # => 130
1903
+ # h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5}
1904
+ # h.sum {|key, value| value.odd? ? value : 0 } # => 9
1905
+ # ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
1906
+ #
1907
+ def sum: () -> (E | Integer)
1908
+ | [T] () { (E arg0) -> T } -> (Integer | T)
1909
+ | [T] (?T arg0) -> (E | T)
1910
+ | [U] (?U arg0) { (E arg0) -> U } -> U
1911
+
1912
+ # <!--
1913
+ # rdoc-file=enum.c
1914
+ # - filter_map {|element| ... } -> array
1915
+ # - filter_map -> enumerator
1916
+ # -->
1917
+ # Returns an array containing truthy elements returned by the block.
1918
+ #
1919
+ # With a block given, calls the block with successive elements; returns an array
1920
+ # containing each truthy value returned by the block:
1921
+ #
1922
+ # (0..9).filter_map {|i| i * 2 if i.even? } # => [0, 4, 8, 12, 16]
1923
+ # {foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? } # => [:foo, :baz]
1924
+ #
1925
+ # When no block given, returns an Enumerator.
1926
+ #
1927
+ def filter_map: [U] () { (E elem) -> (nil | false | U) } -> ::Array[U]
1928
+ | () -> ::Enumerator[E, ::Array[untyped]]
1929
+
1930
+ # <!--
1931
+ # rdoc-file=enumerator.c
1932
+ # - e.chain(*enums) -> enumerator
1933
+ # -->
1934
+ # Returns an enumerator object generated from this enumerator and given
1935
+ # enumerables.
1936
+ #
1937
+ # e = (1..3).chain([4, 5])
1938
+ # e.to_a #=> [1, 2, 3, 4, 5]
1939
+ #
1940
+ def chain: [Elem2] (*_Each[Elem2] enumerables) -> ::Enumerator::Chain[E | Elem2]
1941
+
1942
+ # <!--
1943
+ # rdoc-file=enum.c
1944
+ # - tally(hash = {}) -> hash
1945
+ # -->
1946
+ # When argument `hash` is not given, returns a new hash whose keys are the
1947
+ # distinct elements in `self`; each integer value is the count of occurrences of
1948
+ # each element:
1949
+ #
1950
+ # %w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}
1951
+ #
1952
+ # When argument `hash` is given, returns `hash`, possibly augmented; for each
1953
+ # element `ele` in `self`:
1954
+ #
1955
+ # * Adds it as a key with a zero value if that key does not already exist:
1956
+ #
1957
+ # hash[ele] = 0 unless hash.include?(ele)
1958
+ #
1959
+ # * Increments the value of key `ele`:
1960
+ #
1961
+ # hash[ele] += 1
1962
+ #
1963
+ # This is useful for accumulating tallies across multiple enumerables:
1964
+ #
1965
+ # h = {} # => {}
1966
+ # %w[a c d b c a].tally(h) # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
1967
+ # %w[b a z].tally(h) # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
1968
+ # %w[b a m].tally(h) # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=>1}
1969
+ #
1970
+ # The key to be added or found for an element depends on the class of `self`;
1971
+ # see [Enumerable in Ruby
1972
+ # Classes](rdoc-ref:Enumerable@Enumerable+in+Ruby+Classes).
1973
+ #
1974
+ # Examples:
1975
+ #
1976
+ # * Array (and certain array-like classes): the key is the element (as above).
1977
+ # * Hash (and certain hash-like classes): the key is the 2-element array
1978
+ # formed from the key-value pair:
1979
+ #
1980
+ # h = {} # => {}
1981
+ # {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1}
1982
+ # {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1, [:foo, "c"]=>1, [:bar, "d"]=>1}
1983
+ # {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1}
1984
+ # {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2}
1985
+ #
1986
+ def tally: (?Hash[E, Integer] hash) -> ::Hash[E, Integer]
1987
+
1988
+ # <!--
1989
+ # rdoc-file=enum.c
1990
+ # - each_entry(*args) {|element| ... } -> self
1991
+ # - each_entry(*args) -> enumerator
1992
+ # -->
1993
+ # Calls the given block with each element, converting multiple values from yield
1994
+ # to an array; returns `self`:
1995
+ #
1996
+ # a = []
1997
+ # (1..4).each_entry {|element| a.push(element) } # => 1..4
1998
+ # a # => [1, 2, 3, 4]
1999
+ #
2000
+ # a = []
2001
+ # h = {foo: 0, bar: 1, baz:2}
2002
+ # h.each_entry {|element| a.push(element) }
2003
+ # # => {:foo=>0, :bar=>1, :baz=>2}
2004
+ # a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
2005
+ #
2006
+ # class Foo
2007
+ # include Enumerable
2008
+ # def each
2009
+ # yield 1
2010
+ # yield 1, 2
2011
+ # yield
2012
+ # end
2013
+ # end
2014
+ # Foo.new.each_entry {|yielded| p yielded }
2015
+ #
2016
+ # Output:
2017
+ #
2018
+ # 1
2019
+ # [1, 2]
2020
+ # nil
2021
+ #
2022
+ # With no block given, returns an Enumerator.
2023
+ #
2024
+ def each_entry: () -> ::Enumerator[E, self]
2025
+ | () { (E arg0) -> untyped } -> self
2026
+
2027
+ # <!--
2028
+ # rdoc-file=enum.c
2029
+ # - zip(*other_enums) -> array
2030
+ # - zip(*other_enums) {|array| ... } -> nil
2031
+ # -->
2032
+ # With no block given, returns a new array `new_array` of size self.size whose
2033
+ # elements are arrays. Each nested array <code>new_array[n]</code> is of size
2034
+ # <code>other_enums.size+1</code>, and contains:
2035
+ #
2036
+ # * The `n`-th element of self.
2037
+ # * The `n`-th element of each of the `other_enums`.
2038
+ #
2039
+ # If all `other_enums` and self are the same size, all elements are included in
2040
+ # the result, and there is no `nil`-filling:
2041
+ #
2042
+ # a = [:a0, :a1, :a2, :a3]
2043
+ # b = [:b0, :b1, :b2, :b3]
2044
+ # c = [:c0, :c1, :c2, :c3]
2045
+ # d = a.zip(b, c)
2046
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
2047
+ #
2048
+ # f = {foo: 0, bar: 1, baz: 2}
2049
+ # g = {goo: 3, gar: 4, gaz: 5}
2050
+ # h = {hoo: 6, har: 7, haz: 8}
2051
+ # d = f.zip(g, h)
2052
+ # d # => [
2053
+ # # [[:foo, 0], [:goo, 3], [:hoo, 6]],
2054
+ # # [[:bar, 1], [:gar, 4], [:har, 7]],
2055
+ # # [[:baz, 2], [:gaz, 5], [:haz, 8]]
2056
+ # # ]
2057
+ #
2058
+ # If any enumerable in other_enums is smaller than self, fills to
2059
+ # <code>self.size</code> with `nil`:
2060
+ #
2061
+ # a = [:a0, :a1, :a2, :a3]
2062
+ # b = [:b0, :b1, :b2]
2063
+ # c = [:c0, :c1]
2064
+ # d = a.zip(b, c)
2065
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
2066
+ #
2067
+ # If any enumerable in other_enums is larger than self, its trailing elements
2068
+ # are ignored:
2069
+ #
2070
+ # a = [:a0, :a1, :a2, :a3]
2071
+ # b = [:b0, :b1, :b2, :b3, :b4]
2072
+ # c = [:c0, :c1, :c2, :c3, :c4, :c5]
2073
+ # d = a.zip(b, c)
2074
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
2075
+ #
2076
+ # When a block is given, calls the block with each of the sub-arrays (formed as
2077
+ # above); returns nil:
2078
+ #
2079
+ # a = [:a0, :a1, :a2, :a3]
2080
+ # b = [:b0, :b1, :b2, :b3]
2081
+ # c = [:c0, :c1, :c2, :c3]
2082
+ # a.zip(b, c) {|sub_array| p sub_array} # => nil
2083
+ #
2084
+ # Output:
2085
+ #
2086
+ # [:a0, :b0, :c0]
2087
+ # [:a1, :b1, :c1]
2088
+ # [:a2, :b2, :c2]
2089
+ # [:a3, :b3, :c3]
2090
+ #
2091
+ def zip: [Elem2] (_Each[Elem2] enum) -> Array[[ E, Elem2? ]]
2092
+ | (_Each[untyped], *_Each[untyped]) -> Array[Array[untyped]]
2093
+ | [Elem2] (_Each[Elem2]) { ([ E, Elem2? ]) -> void } -> nil
2094
+ | (_Each[untyped], *_Each[untyped]) { (Array[untyped]) -> void } -> nil
2095
+
2096
+ # <!--
2097
+ # rdoc-file=enum.c
2098
+ # - chunk {|array| ... } -> enumerator
2099
+ # -->
2100
+ # Each element in the returned enumerator is a 2-element array consisting of:
2101
+ #
2102
+ # * A value returned by the block.
2103
+ # * An array ("chunk") containing the element for which that value was
2104
+ # returned, and all following elements for which the block returned the same
2105
+ # value:
2106
+ #
2107
+ # So that:
2108
+ #
2109
+ # * Each block return value that is different from its predecessor begins a
2110
+ # new chunk.
2111
+ # * Each block return value that is the same as its predecessor continues the
2112
+ # same chunk.
2113
+ #
2114
+ # Example:
2115
+ #
2116
+ # e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
2117
+ # # The enumerator elements.
2118
+ # e.next # => [0, [0, 1, 2]]
2119
+ # e.next # => [1, [3, 4, 5]]
2120
+ # e.next # => [2, [6, 7, 8]]
2121
+ # e.next # => [3, [9, 10]]
2122
+ #
2123
+ # Method `chunk` is especially useful for an enumerable that is already sorted.
2124
+ # This example counts words for each initial letter in a large array of words:
2125
+ #
2126
+ # # Get sorted words from a web page.
2127
+ # url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt'
2128
+ # words = URI::open(url).readlines
2129
+ # # Make chunks, one for each letter.
2130
+ # e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...>
2131
+ # # Display 'A' through 'F'.
2132
+ # e.each {|c, words| p [c, words.length]; break if c == 'F' }
2133
+ #
2134
+ # Output:
2135
+ #
2136
+ # ["A", 17096]
2137
+ # ["B", 11070]
2138
+ # ["C", 19901]
2139
+ # ["D", 10896]
2140
+ # ["E", 8736]
2141
+ # ["F", 6860]
2142
+ #
2143
+ # You can use the special symbol <code>:_alone</code> to force an element into
2144
+ # its own separate chunk:
2145
+ #
2146
+ # a = [0, 0, 1, 1]
2147
+ # e = a.chunk{|i| i.even? ? :_alone : true }
2148
+ # e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
2149
+ #
2150
+ # For example, you can put each line that contains a URL into its own chunk:
2151
+ #
2152
+ # pattern = /http/
2153
+ # open(filename) { |f|
2154
+ # f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
2155
+ # pp lines
2156
+ # }
2157
+ # }
2158
+ #
2159
+ # You can use the special symbol <code>:_separator</code> or `nil` to force an
2160
+ # element to be ignored (not included in any chunk):
2161
+ #
2162
+ # a = [0, 0, -1, 1, 1]
2163
+ # e = a.chunk{|i| i < 0 ? :_separator : true }
2164
+ # e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
2165
+ #
2166
+ # Note that the separator does end the chunk:
2167
+ #
2168
+ # a = [0, 0, -1, 1, -1, 1]
2169
+ # e = a.chunk{|i| i < 0 ? :_separator : true }
2170
+ # e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]
2171
+ #
2172
+ # For example, the sequence of hyphens in svn log can be eliminated as follows:
2173
+ #
2174
+ # sep = "-"*72 + "\n"
2175
+ # IO.popen("svn log README") { |f|
2176
+ # f.chunk { |line|
2177
+ # line != sep || nil
2178
+ # }.each { |_, lines|
2179
+ # pp lines
2180
+ # }
2181
+ # }
2182
+ # #=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n",
2183
+ # # "\n",
2184
+ # # "* README, README.ja: Update the portability section.\n",
2185
+ # # "\n"]
2186
+ # # ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n",
2187
+ # # "\n",
2188
+ # # "* README, README.ja: Add a note about default C flags.\n",
2189
+ # # "\n"]
2190
+ # # ...
2191
+ #
2192
+ # Paragraphs separated by empty lines can be parsed as follows:
2193
+ #
2194
+ # File.foreach("README").chunk { |line|
2195
+ # /\A\s*\z/ !~ line || nil
2196
+ # }.each { |_, lines|
2197
+ # pp lines
2198
+ # }
2199
+ #
2200
+ def chunk: [U] () { (E elt) -> U } -> ::Enumerator[[ U, ::Array[E] ]]
2201
+ | () -> ::Enumerator[E, ::Enumerator[[ untyped, ::Array[E] ]]]
2202
+
2203
+ # <!--
2204
+ # rdoc-file=enum.c
2205
+ # - enum.chunk_while {|elt_before, elt_after| bool } -> an_enumerator
2206
+ # -->
2207
+ # Creates an enumerator for each chunked elements. The beginnings of chunks are
2208
+ # defined by the block.
2209
+ #
2210
+ # This method splits each chunk using adjacent elements, *elt_before* and
2211
+ # *elt_after*, in the receiver enumerator. This method split chunks between
2212
+ # *elt_before* and *elt_after* where the block returns `false`.
2213
+ #
2214
+ # The block is called the length of the receiver enumerator minus one.
2215
+ #
2216
+ # The result enumerator yields the chunked elements as an array. So `each`
2217
+ # method can be called as follows:
2218
+ #
2219
+ # enum.chunk_while { |elt_before, elt_after| bool }.each { |ary| ... }
2220
+ #
2221
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2222
+ # `map`, etc., are also usable.
2223
+ #
2224
+ # For example, one-by-one increasing subsequence can be chunked as follows:
2225
+ #
2226
+ # a = [1,2,4,9,10,11,12,15,16,19,20,21]
2227
+ # b = a.chunk_while {|i, j| i+1 == j }
2228
+ # p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
2229
+ # c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
2230
+ # p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
2231
+ # d = c.join(",")
2232
+ # p d #=> "1,2,4,9-12,15,16,19-21"
2233
+ #
2234
+ # Increasing (non-decreasing) subsequence can be chunked as follows:
2235
+ #
2236
+ # a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
2237
+ # p a.chunk_while {|i, j| i <= j }.to_a
2238
+ # #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
2239
+ #
2240
+ # Adjacent evens and odds can be chunked as follows: (Enumerable#chunk is
2241
+ # another way to do it.)
2242
+ #
2243
+ # a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
2244
+ # p a.chunk_while {|i, j| i.even? == j.even? }.to_a
2245
+ # #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
2246
+ #
2247
+ # Enumerable#slice_when does the same, except splitting when the block returns
2248
+ # `true` instead of `false`.
2249
+ #
2250
+ def chunk_while: () { (E elt_before, E elt_after) -> boolish } -> ::Enumerator[::Array[E]]
2251
+
2252
+ # <!--
2253
+ # rdoc-file=enum.c
2254
+ # - enum.slice_when {|elt_before, elt_after| bool } -> an_enumerator
2255
+ # -->
2256
+ # Creates an enumerator for each chunked elements. The beginnings of chunks are
2257
+ # defined by the block.
2258
+ #
2259
+ # This method splits each chunk using adjacent elements, *elt_before* and
2260
+ # *elt_after*, in the receiver enumerator. This method split chunks between
2261
+ # *elt_before* and *elt_after* where the block returns `true`.
2262
+ #
2263
+ # The block is called the length of the receiver enumerator minus one.
2264
+ #
2265
+ # The result enumerator yields the chunked elements as an array. So `each`
2266
+ # method can be called as follows:
2267
+ #
2268
+ # enum.slice_when { |elt_before, elt_after| bool }.each { |ary| ... }
2269
+ #
2270
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2271
+ # `map`, etc., are also usable.
2272
+ #
2273
+ # For example, one-by-one increasing subsequence can be chunked as follows:
2274
+ #
2275
+ # a = [1,2,4,9,10,11,12,15,16,19,20,21]
2276
+ # b = a.slice_when {|i, j| i+1 != j }
2277
+ # p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
2278
+ # c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
2279
+ # p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
2280
+ # d = c.join(",")
2281
+ # p d #=> "1,2,4,9-12,15,16,19-21"
2282
+ #
2283
+ # Near elements (threshold: 6) in sorted array can be chunked as follows:
2284
+ #
2285
+ # a = [3, 11, 14, 25, 28, 29, 29, 41, 55, 57]
2286
+ # p a.slice_when {|i, j| 6 < j - i }.to_a
2287
+ # #=> [[3], [11, 14], [25, 28, 29, 29], [41], [55, 57]]
2288
+ #
2289
+ # Increasing (non-decreasing) subsequence can be chunked as follows:
2290
+ #
2291
+ # a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
2292
+ # p a.slice_when {|i, j| i > j }.to_a
2293
+ # #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
2294
+ #
2295
+ # Adjacent evens and odds can be chunked as follows: (Enumerable#chunk is
2296
+ # another way to do it.)
2297
+ #
2298
+ # a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
2299
+ # p a.slice_when {|i, j| i.even? != j.even? }.to_a
2300
+ # #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
2301
+ #
2302
+ # Paragraphs (non-empty lines with trailing empty lines) can be chunked as
2303
+ # follows: (See Enumerable#chunk to ignore empty lines.)
2304
+ #
2305
+ # lines = ["foo\n", "bar\n", "\n", "baz\n", "qux\n"]
2306
+ # p lines.slice_when {|l1, l2| /\A\s*\z/ =~ l1 && /\S/ =~ l2 }.to_a
2307
+ # #=> [["foo\n", "bar\n", "\n"], ["baz\n", "qux\n"]]
2308
+ #
2309
+ # Enumerable#chunk_while does the same, except splitting when the block returns
2310
+ # `false` instead of `true`.
2311
+ #
2312
+ def slice_when: () { (E elt_before, E elt_after) -> boolish } -> ::Enumerator[::Array[E]]
2313
+
2314
+ # <!--
2315
+ # rdoc-file=enum.c
2316
+ # - enum.slice_after(pattern) -> an_enumerator
2317
+ # - enum.slice_after { |elt| bool } -> an_enumerator
2318
+ # -->
2319
+ # Creates an enumerator for each chunked elements. The ends of chunks are
2320
+ # defined by *pattern* and the block.
2321
+ #
2322
+ # If <code>_pattern_ === _elt_</code> returns `true` or the block returns `true`
2323
+ # for the element, the element is end of a chunk.
2324
+ #
2325
+ # The <code>===</code> and *block* is called from the first element to the last
2326
+ # element of *enum*.
2327
+ #
2328
+ # The result enumerator yields the chunked elements as an array. So `each`
2329
+ # method can be called as follows:
2330
+ #
2331
+ # enum.slice_after(pattern).each { |ary| ... }
2332
+ # enum.slice_after { |elt| bool }.each { |ary| ... }
2333
+ #
2334
+ # Other methods of the Enumerator class and Enumerable module, such as `map`,
2335
+ # etc., are also usable.
2336
+ #
2337
+ # For example, continuation lines (lines end with backslash) can be concatenated
2338
+ # as follows:
2339
+ #
2340
+ # lines = ["foo\n", "bar\\\n", "baz\n", "\n", "qux\n"]
2341
+ # e = lines.slice_after(/(?<!\\)\n\z/)
2342
+ # p e.to_a
2343
+ # #=> [["foo\n"], ["bar\\\n", "baz\n"], ["\n"], ["qux\n"]]
2344
+ # p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
2345
+ # #=>["foo\n", "barbaz\n", "\n", "qux\n"]
2346
+ #
2347
+ def slice_after: (untyped pattern) -> ::Enumerator[::Array[E]]
2348
+ | () { (E elt) -> boolish } -> ::Enumerator[::Array[E]]
2349
+
2350
+ # <!--
2351
+ # rdoc-file=enum.c
2352
+ # - slice_before(pattern) -> enumerator
2353
+ # - slice_before {|elt| ... } -> enumerator
2354
+ # -->
2355
+ # With argument `pattern`, returns an enumerator that uses the pattern to
2356
+ # partition elements into arrays ("slices"). An element begins a new slice if
2357
+ # <code>element === pattern</code> (or if it is the first element).
2358
+ #
2359
+ # a = %w[foo bar fop for baz fob fog bam foy]
2360
+ # e = a.slice_before(/ba/) # => #<Enumerator: ...>
2361
+ # e.each {|array| p array }
2362
+ #
2363
+ # Output:
2364
+ #
2365
+ # ["foo"]
2366
+ # ["bar", "fop", "for"]
2367
+ # ["baz", "fob", "fog"]
2368
+ # ["bam", "foy"]
2369
+ #
2370
+ # With a block, returns an enumerator that uses the block to partition elements
2371
+ # into arrays. An element begins a new slice if its block return is a truthy
2372
+ # value (or if it is the first element):
2373
+ #
2374
+ # e = (1..20).slice_before {|i| i % 4 == 2 } # => #<Enumerator: ...>
2375
+ # e.each {|array| p array }
2376
+ #
2377
+ # Output:
2378
+ #
2379
+ # [1]
2380
+ # [2, 3, 4, 5]
2381
+ # [6, 7, 8, 9]
2382
+ # [10, 11, 12, 13]
2383
+ # [14, 15, 16, 17]
2384
+ # [18, 19, 20]
2385
+ #
2386
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2387
+ # `map`, etc., are also usable.
2388
+ #
2389
+ # For example, iteration over ChangeLog entries can be implemented as follows:
2390
+ #
2391
+ # # iterate over ChangeLog entries.
2392
+ # open("ChangeLog") { |f|
2393
+ # f.slice_before(/\A\S/).each { |e| pp e }
2394
+ # }
2395
+ #
2396
+ # # same as above. block is used instead of pattern argument.
2397
+ # open("ChangeLog") { |f|
2398
+ # f.slice_before { |line| /\A\S/ === line }.each { |e| pp e }
2399
+ # }
2400
+ #
2401
+ # "svn proplist -R" produces multiline output for each file. They can be chunked
2402
+ # as follows:
2403
+ #
2404
+ # IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) { |f|
2405
+ # f.lines.slice_before(/\AProp/).each { |lines| p lines }
2406
+ # }
2407
+ # #=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
2408
+ # # ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
2409
+ # # ["Properties on 'complex.c':\n", " svn:mime-type\n", " svn:eol-style\n"]
2410
+ # # ["Properties on 'regparse.c':\n", " svn:eol-style\n"]
2411
+ # # ...
2412
+ #
2413
+ # If the block needs to maintain state over multiple elements, local variables
2414
+ # can be used. For example, three or more consecutive increasing numbers can be
2415
+ # squashed as follows (see `chunk_while` for a better way):
2416
+ #
2417
+ # a = [0, 2, 3, 4, 6, 7, 9]
2418
+ # prev = a[0]
2419
+ # p a.slice_before { |e|
2420
+ # prev, prev2 = e, prev
2421
+ # prev2 + 1 != e
2422
+ # }.map { |es|
2423
+ # es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
2424
+ # }.join(",")
2425
+ # #=> "0,2-4,6,7,9"
2426
+ #
2427
+ # However local variables should be used carefully if the result enumerator is
2428
+ # enumerated twice or more. The local variables should be initialized for each
2429
+ # enumeration. Enumerator.new can be used to do it.
2430
+ #
2431
+ # # Word wrapping. This assumes all characters have same width.
2432
+ # def wordwrap(words, maxwidth)
2433
+ # Enumerator.new {|y|
2434
+ # # cols is initialized in Enumerator.new.
2435
+ # cols = 0
2436
+ # words.slice_before { |w|
2437
+ # cols += 1 if cols != 0
2438
+ # cols += w.length
2439
+ # if maxwidth < cols
2440
+ # cols = w.length
2441
+ # true
2442
+ # else
2443
+ # false
2444
+ # end
2445
+ # }.each {|ws| y.yield ws }
2446
+ # }
2447
+ # end
2448
+ # text = (1..20).to_a.join(" ")
2449
+ # enum = wordwrap(text.split(/\s+/), 10)
2450
+ # puts "-"*10
2451
+ # enum.each { |ws| puts ws.join(" ") } # first enumeration.
2452
+ # puts "-"*10
2453
+ # enum.each { |ws| puts ws.join(" ") } # second enumeration generates same result as the first.
2454
+ # puts "-"*10
2455
+ # #=> ----------
2456
+ # # 1 2 3 4 5
2457
+ # # 6 7 8 9 10
2458
+ # # 11 12 13
2459
+ # # 14 15 16
2460
+ # # 17 18 19
2461
+ # # 20
2462
+ # # ----------
2463
+ # # 1 2 3 4 5
2464
+ # # 6 7 8 9 10
2465
+ # # 11 12 13
2466
+ # # 14 15 16
2467
+ # # 17 18 19
2468
+ # # 20
2469
+ # # ----------
2470
+ #
2471
+ # mbox contains series of mails which start with Unix From line. So each mail
2472
+ # can be extracted by slice before Unix From line.
2473
+ #
2474
+ # # parse mbox
2475
+ # open("mbox") { |f|
2476
+ # f.slice_before { |line|
2477
+ # line.start_with? "From "
2478
+ # }.each { |mail|
2479
+ # unix_from = mail.shift
2480
+ # i = mail.index("\n")
2481
+ # header = mail[0...i]
2482
+ # body = mail[(i+1)..-1]
2483
+ # body.pop if body.last == "\n"
2484
+ # fields = header.slice_before { |line| !" \t".include?(line[0]) }.to_a
2485
+ # p unix_from
2486
+ # pp fields
2487
+ # pp body
2488
+ # }
2489
+ # }
2490
+ #
2491
+ # # split mails in mbox (slice before Unix From line after an empty line)
2492
+ # open("mbox") { |f|
2493
+ # emp = true
2494
+ # f.slice_before { |line|
2495
+ # prevemp = emp
2496
+ # emp = line == "\n"
2497
+ # prevemp && line.start_with?("From ")
2498
+ # }.each { |mail|
2499
+ # mail.pop if mail.last == "\n"
2500
+ # pp mail
2501
+ # }
2502
+ # }
2503
+ #
2504
+ def slice_before: (untyped pattern) -> ::Enumerator[::Array[E]]
2505
+ | () { (E elt) -> boolish } -> ::Enumerator[::Array[E]]
2506
+ end