rbs 3.9.4 → 4.1.1
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.
- checksums.yaml +4 -4
- data/.clang-format +75 -0
- data/.clangd +2 -0
- data/.dockerignore +37 -0
- data/.github/dependabot.yml +16 -14
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +66 -0
- data/.github/workflows/comments.yml +5 -3
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/jruby.yml +79 -0
- data/.github/workflows/release-gems.yml +164 -0
- data/.github/workflows/ruby.yml +84 -30
- data/.github/workflows/rust.yml +186 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +5 -2
- data/.github/workflows/wasm.yml +55 -0
- data/.github/workflows/windows.yml +9 -3
- data/.gitignore +16 -0
- data/.rubocop.yml +2 -2
- data/CHANGELOG.md +430 -0
- data/Dockerfile.jruby +53 -0
- data/README.md +41 -4
- data/Rakefile +777 -60
- data/Steepfile +11 -0
- data/config.yml +660 -62
- data/core/array.rbs +541 -398
- data/core/basic_object.rbs +9 -8
- data/core/binding.rbs +0 -2
- data/core/builtin.rbs +9 -8
- data/core/class.rbs +11 -8
- data/core/comparable.rbs +55 -34
- data/core/complex.rbs +104 -78
- data/core/dir.rbs +61 -49
- data/core/encoding.rbs +12 -15
- data/core/enumerable.rbs +297 -196
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +91 -28
- data/core/errno.rbs +11 -2
- data/core/errors.rbs +58 -29
- data/core/exception.rbs +13 -13
- data/core/fiber.rbs +74 -54
- data/core/file.rbs +260 -1151
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/file_test.rbs +3 -3
- data/core/float.rbs +257 -116
- data/core/gc.rbs +425 -281
- data/core/hash.rbs +1151 -829
- data/core/integer.rbs +156 -195
- data/core/io/buffer.rbs +53 -42
- data/core/io/wait.rbs +13 -35
- data/core/io.rbs +220 -154
- data/core/kernel.rbs +239 -163
- data/core/marshal.rbs +4 -4
- data/core/match_data.rbs +16 -14
- data/core/math.rbs +107 -66
- data/core/method.rbs +69 -33
- data/core/module.rbs +302 -150
- data/core/nil_class.rbs +7 -6
- data/core/numeric.rbs +77 -63
- data/core/object.rbs +9 -11
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/object_space.rbs +30 -23
- data/core/pathname.rbs +1312 -0
- data/core/proc.rbs +95 -58
- data/core/process.rbs +222 -202
- data/core/ractor.rbs +364 -518
- data/core/random.rbs +21 -3
- data/core/range.rbs +181 -79
- data/core/rational.rbs +60 -89
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +63 -56
- data/core/rbs/unnamed/env_class.rbs +19 -14
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +11 -118
- data/core/regexp.rbs +258 -214
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +78 -34
- data/core/rubygems/config_file.rbs +5 -5
- data/core/rubygems/errors.rbs +6 -70
- data/core/rubygems/requirement.rbs +5 -15
- data/core/rubygems/rubygems.rbs +18 -81
- data/core/rubygems/specification.rbs +8 -0
- data/core/rubygems/version.rbs +2 -163
- data/core/set.rbs +493 -363
- data/core/signal.rbs +26 -16
- data/core/string.rbs +3234 -1285
- data/core/struct.rbs +43 -42
- data/core/symbol.rbs +41 -34
- data/core/thread.rbs +139 -83
- data/core/time.rbs +81 -50
- data/core/trace_point.rbs +41 -35
- data/core/true_class.rbs +2 -2
- data/core/unbound_method.rbs +24 -16
- data/core/warning.rbs +7 -7
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/aliases.md +79 -0
- data/docs/collection.md +3 -3
- data/docs/config.md +171 -0
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/inline.md +634 -0
- data/docs/rbs_by_example.md +20 -20
- data/docs/release.md +151 -0
- data/docs/rust.md +96 -0
- data/docs/sigs.md +3 -3
- data/docs/syntax.md +48 -18
- data/docs/type_fingerprint.md +21 -0
- data/docs/wasm_serialization.md +80 -0
- data/exe/rbs +1 -1
- data/ext/rbs_extension/ast_translation.c +1855 -0
- data/ext/rbs_extension/ast_translation.h +41 -0
- data/ext/rbs_extension/class_constants.c +187 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +23 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +26 -1
- data/ext/rbs_extension/legacy_location.c +299 -0
- data/ext/rbs_extension/legacy_location.h +82 -0
- data/ext/rbs_extension/main.c +603 -23
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +1047 -0
- data/include/rbs/defines.h +99 -0
- data/include/rbs/lexer.h +207 -0
- data/include/rbs/location.h +40 -0
- data/include/rbs/parser.h +153 -0
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -70
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +9 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +30 -32
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/ast/annotation.rb +1 -1
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/ast/declarations.rb +10 -10
- data/lib/rbs/ast/members.rb +14 -14
- data/lib/rbs/ast/ruby/annotations.rb +451 -0
- data/lib/rbs/ast/ruby/comment_block.rb +247 -0
- data/lib/rbs/ast/ruby/declarations.rb +291 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +762 -0
- data/lib/rbs/ast/type_param.rb +24 -4
- data/lib/rbs/buffer.rb +142 -20
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +63 -126
- data/lib/rbs/cli.rb +59 -29
- data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
- data/lib/rbs/collection/sources/git.rb +7 -0
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +121 -65
- data/lib/rbs/definition_builder/method_builder.rb +74 -33
- data/lib/rbs/definition_builder.rb +177 -20
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment/class_entry.rb +69 -0
- data/lib/rbs/environment/module_entry.rb +66 -0
- data/lib/rbs/environment.rb +410 -215
- data/lib/rbs/environment_loader.rb +2 -8
- data/lib/rbs/errors.rb +31 -21
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +568 -0
- data/lib/rbs/location_aux.rb +36 -4
- data/lib/rbs/locator.rb +5 -1
- data/lib/rbs/method_type.rb +5 -3
- data/lib/rbs/namespace.rb +47 -18
- data/lib/rbs/parser_aux.rb +37 -7
- data/lib/rbs/prototype/helpers.rb +57 -0
- data/lib/rbs/prototype/rb.rb +3 -28
- data/lib/rbs/prototype/rbi.rb +196 -45
- data/lib/rbs/prototype/runtime.rb +12 -2
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +120 -44
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/subtractor.rb +7 -4
- data/lib/rbs/test/type_check.rb +25 -3
- data/lib/rbs/type_name.rb +34 -21
- data/lib/rbs/types.rb +91 -79
- data/lib/rbs/unit_test/convertibles.rb +1 -0
- data/lib/rbs/unit_test/type_assertions.rb +44 -8
- data/lib/rbs/validator.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +196 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +25 -2
- data/lib/rbs_jars.rb +39 -0
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +8 -3
- data/rbs.gemspec +37 -5
- data/schema/typeParam.json +17 -1
- data/sig/ancestor_builder.rbs +1 -1
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +13 -10
- data/sig/ast/ruby/annotations.rbs +470 -0
- data/sig/ast/ruby/comment_block.rbs +127 -0
- data/sig/ast/ruby/declarations.rbs +158 -0
- data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
- data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
- data/sig/ast/ruby/members.rbs +198 -0
- data/sig/buffer.rbs +81 -5
- data/sig/cli/diff.rbs +5 -11
- data/sig/cli/validate.rbs +12 -8
- data/sig/cli.rbs +18 -18
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/definition.rbs +6 -0
- data/sig/definition_builder.rbs +3 -1
- data/sig/environment/class_entry.rbs +50 -0
- data/sig/environment/module_entry.rbs +50 -0
- data/sig/environment.rbs +94 -87
- data/sig/errors.rbs +26 -20
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +126 -0
- data/sig/location.rbs +32 -7
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -2
- data/sig/method_builder.rbs +9 -4
- data/sig/namespace.rbs +20 -5
- data/sig/parser.rbs +77 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/prototype/rbi.rbs +33 -4
- data/sig/resolver/type_name_resolver.rbs +36 -10
- data/sig/rewriter.rbs +45 -0
- data/sig/source.rbs +48 -0
- data/sig/type_param.rbs +13 -8
- data/sig/typename.rbs +15 -5
- data/sig/types.rbs +10 -8
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +15 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +1628 -0
- data/src/lexer.c +3221 -0
- data/src/lexer.re +155 -0
- data/src/lexstate.c +221 -0
- data/src/location.c +31 -0
- data/src/parser.c +4258 -0
- data/src/serialize.c +958 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +171 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +18 -92
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +167 -0
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +9 -393
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +171 -0
- data/stdlib/coverage/0/coverage.rbs +7 -4
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/date/0/date.rbs +92 -79
- data/stdlib/date/0/date_time.rbs +25 -24
- data/stdlib/delegate/0/delegator.rbs +11 -7
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/digest/0/digest.rbs +117 -1
- data/stdlib/erb/0/erb.rbs +748 -347
- data/stdlib/etc/0/etc.rbs +73 -54
- data/stdlib/fileutils/0/fileutils.rbs +179 -160
- data/stdlib/forwardable/0/forwardable.rbs +13 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/ipaddr/0/ipaddr.rbs +0 -5
- data/stdlib/json/0/json.rbs +232 -185
- data/stdlib/monitor/0/monitor.rbs +5 -5
- data/stdlib/net-http/0/net-http.rbs +162 -134
- data/stdlib/objspace/0/objspace.rbs +17 -34
- data/stdlib/open-uri/0/open-uri.rbs +48 -8
- data/stdlib/open3/0/open3.rbs +469 -10
- data/stdlib/openssl/0/openssl.rbs +521 -397
- data/stdlib/optparse/0/optparse.rbs +26 -17
- data/stdlib/pathname/0/pathname.rbs +11 -1381
- data/stdlib/pp/0/pp.rbs +9 -8
- data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
- data/stdlib/pstore/0/pstore.rbs +35 -30
- data/stdlib/psych/0/psych.rbs +65 -12
- data/stdlib/psych/0/store.rbs +2 -4
- data/stdlib/pty/0/pty.rbs +9 -6
- data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
- data/stdlib/rdoc/0/code_object.rbs +4 -3
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/parser.rbs +1 -1
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +2 -2
- data/stdlib/resolv/0/resolv.rbs +26 -69
- data/stdlib/ripper/0/ripper.rbs +25 -19
- data/stdlib/securerandom/0/manifest.yaml +2 -0
- data/stdlib/securerandom/0/securerandom.rbs +7 -20
- data/stdlib/shellwords/0/shellwords.rbs +3 -3
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +9 -9
- data/stdlib/socket/0/basic_socket.rbs +3 -3
- data/stdlib/socket/0/ip_socket.rbs +10 -8
- data/stdlib/socket/0/socket.rbs +23 -10
- data/stdlib/socket/0/tcp_server.rbs +1 -1
- data/stdlib/socket/0/tcp_socket.rbs +11 -3
- data/stdlib/socket/0/udp_socket.rbs +1 -1
- data/stdlib/socket/0/unix_server.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +1209 -95
- data/stdlib/strscan/0/string_scanner.rbs +101 -80
- data/stdlib/tempfile/0/manifest.yaml +3 -0
- data/stdlib/tempfile/0/tempfile.rbs +25 -21
- data/stdlib/time/0/time.rbs +8 -6
- data/stdlib/timeout/0/timeout.rbs +58 -7
- data/stdlib/tsort/0/cyclic.rbs +4 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +16 -15
- data/stdlib/uri/0/common.rbs +42 -20
- data/stdlib/uri/0/file.rbs +3 -3
- data/stdlib/uri/0/generic.rbs +21 -18
- data/stdlib/uri/0/http.rbs +2 -2
- data/stdlib/uri/0/ldap.rbs +2 -2
- data/stdlib/uri/0/mailto.rbs +3 -3
- data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
- data/stdlib/zlib/0/deflate.rbs +4 -3
- data/stdlib/zlib/0/gzip_reader.rbs +8 -8
- data/stdlib/zlib/0/gzip_writer.rbs +14 -12
- data/stdlib/zlib/0/inflate.rbs +1 -1
- data/stdlib/zlib/0/need_dict.rbs +1 -1
- data/wasm/README.md +60 -0
- data/wasm/rbs_wasm.c +423 -0
- metadata +131 -19
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/core/enumerable.rbs
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
# These methods return information about the Enumerable other than the elements
|
|
17
17
|
# themselves:
|
|
18
18
|
#
|
|
19
|
-
# * #member? (aliased as #include?): Returns `true` if
|
|
20
|
-
# `false` otherwise.
|
|
19
|
+
# * #member? (aliased as #include?): Returns `true` if <code>self ==
|
|
20
|
+
# object</code>, `false` otherwise.
|
|
21
21
|
# * #all?: Returns `true` if all elements meet a specified criterion; `false`
|
|
22
22
|
# otherwise.
|
|
23
23
|
# * #any?: Returns `true` if any element meets a specified criterion; `false`
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
#
|
|
36
36
|
# These methods return entries from the Enumerable, without modifying it:
|
|
37
37
|
#
|
|
38
|
-
#
|
|
38
|
+
# <em>Leading, trailing, or all elements</em>:
|
|
39
39
|
#
|
|
40
40
|
# * #to_a (aliased as #entries): Returns all elements.
|
|
41
41
|
# * #first: Returns the first element or leading elements.
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
# *Minimum and maximum value elements*:
|
|
48
48
|
#
|
|
49
49
|
# * #min: Returns the elements whose values are smallest among the elements,
|
|
50
|
-
# as determined by
|
|
50
|
+
# as determined by <code>#<=></code> or a given block.
|
|
51
51
|
# * #max: Returns the elements whose values are largest among the elements, as
|
|
52
|
-
# determined by
|
|
52
|
+
# determined by <code>#<=></code> or a given block.
|
|
53
53
|
# * #minmax: Returns a 2-element Array containing the smallest and largest
|
|
54
54
|
# elements.
|
|
55
55
|
# * #min_by: Returns the smallest element, as determined by the given block.
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
# * #minmax_by: Returns the smallest and largest elements, as determined by
|
|
58
58
|
# the given block.
|
|
59
59
|
#
|
|
60
|
-
#
|
|
60
|
+
# <em>Groups, slices, and partitions</em>:
|
|
61
61
|
#
|
|
62
62
|
# * #group_by: Returns a Hash that partitions the elements into groups.
|
|
63
63
|
# * #partition: Returns elements partitioned into two new Arrays, as
|
|
@@ -89,7 +89,8 @@
|
|
|
89
89
|
#
|
|
90
90
|
# These methods return elements in sorted order:
|
|
91
91
|
#
|
|
92
|
-
# * #sort: Returns the elements, sorted by
|
|
92
|
+
# * #sort: Returns the elements, sorted by <code>#<=></code> or the given
|
|
93
|
+
# block.
|
|
93
94
|
# * #sort_by: Returns the elements, sorted by the given block.
|
|
94
95
|
#
|
|
95
96
|
# ### Methods for Iterating
|
|
@@ -114,11 +115,11 @@
|
|
|
114
115
|
# by the block.
|
|
115
116
|
# * #grep: Returns elements selected by a given object or objects returned by
|
|
116
117
|
# a given block.
|
|
117
|
-
# * #grep_v: Returns elements selected by a given object or objects
|
|
118
|
-
# by a given block.
|
|
118
|
+
# * #grep_v: Returns elements not selected by a given object or objects
|
|
119
|
+
# returned by a given block.
|
|
119
120
|
# * #inject (aliased as #reduce): Returns the object formed by combining all
|
|
120
121
|
# elements.
|
|
121
|
-
# * #sum: Returns the sum of the elements, using method
|
|
122
|
+
# * #sum: Returns the sum of the elements, using method <code>+</code>.
|
|
122
123
|
# * #zip: Combines each element with elements from other enumerables; returns
|
|
123
124
|
# the n-tuples or calls the block with each.
|
|
124
125
|
# * #cycle: Calls the block with each element, cycling repeatedly.
|
|
@@ -131,8 +132,9 @@
|
|
|
131
132
|
#
|
|
132
133
|
# include Enumerable
|
|
133
134
|
#
|
|
134
|
-
# * Implement method
|
|
135
|
-
# collection. The method will be called by almost any Enumerable
|
|
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.
|
|
136
138
|
#
|
|
137
139
|
# Example:
|
|
138
140
|
#
|
|
@@ -173,13 +175,15 @@
|
|
|
173
175
|
# * CSV::Row
|
|
174
176
|
# * Set
|
|
175
177
|
#
|
|
176
|
-
# Virtually all methods in Enumerable call method
|
|
177
|
-
# class:
|
|
178
|
+
# Virtually all methods in Enumerable call method <code>#each</code> in the
|
|
179
|
+
# including class:
|
|
178
180
|
#
|
|
179
|
-
# *
|
|
180
|
-
#
|
|
181
|
-
# *
|
|
182
|
-
#
|
|
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.
|
|
183
187
|
#
|
|
184
188
|
# ## About the Examples
|
|
185
189
|
#
|
|
@@ -191,7 +195,88 @@
|
|
|
191
195
|
# usage would not make sense, and so it is not shown. Example: #tally would
|
|
192
196
|
# find exactly one of each Hash entry.
|
|
193
197
|
#
|
|
194
|
-
|
|
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
|
+
|
|
195
280
|
# <!--
|
|
196
281
|
# rdoc-file=enum.c
|
|
197
282
|
# - all? -> true or false
|
|
@@ -211,7 +296,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
211
296
|
# [].all? # => true
|
|
212
297
|
#
|
|
213
298
|
# With argument `pattern` and no block, returns whether for each element
|
|
214
|
-
# `element`,
|
|
299
|
+
# `element`, <code>pattern === element</code>:
|
|
215
300
|
#
|
|
216
301
|
# (1..4).all?(Integer) # => true
|
|
217
302
|
# (1..4).all?(Numeric) # => true
|
|
@@ -234,7 +319,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
234
319
|
# Related: #any?, #none? #one?.
|
|
235
320
|
#
|
|
236
321
|
def all?: () -> bool
|
|
237
|
-
| ()
|
|
322
|
+
| (_Pattern) -> bool
|
|
323
|
+
| () { (E) -> boolish } -> bool
|
|
238
324
|
|
|
239
325
|
# <!--
|
|
240
326
|
# rdoc-file=enum.c
|
|
@@ -254,7 +340,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
254
340
|
# [].any? # => false
|
|
255
341
|
#
|
|
256
342
|
# With argument `pattern` and no block, returns whether for any element
|
|
257
|
-
# `element`,
|
|
343
|
+
# `element`, <code>pattern === element</code>:
|
|
258
344
|
#
|
|
259
345
|
# [nil, false, 0].any?(Integer) # => true
|
|
260
346
|
# [nil, false, 0].any?(Numeric) # => true
|
|
@@ -277,7 +363,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
277
363
|
# Related: #all?, #none?, #one?.
|
|
278
364
|
#
|
|
279
365
|
def any?: () -> bool
|
|
280
|
-
| ()
|
|
366
|
+
| (_Pattern) -> bool
|
|
367
|
+
| () { (E) -> boolish } -> bool
|
|
281
368
|
|
|
282
369
|
# <!--
|
|
283
370
|
# rdoc-file=enum.c
|
|
@@ -294,8 +381,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
294
381
|
#
|
|
295
382
|
# With no block given, returns an Enumerator.
|
|
296
383
|
#
|
|
297
|
-
def collect: [U] () { (
|
|
298
|
-
| () -> ::Enumerator[
|
|
384
|
+
def collect: [U] () { (E arg0) -> U } -> ::Array[U]
|
|
385
|
+
| () -> ::Enumerator[E, ::Array[untyped]]
|
|
299
386
|
|
|
300
387
|
# <!-- rdoc-file=enum.c -->
|
|
301
388
|
# Returns an array of flattened objects returned by the block.
|
|
@@ -312,8 +399,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
312
399
|
#
|
|
313
400
|
# Alias: #collect_concat.
|
|
314
401
|
#
|
|
315
|
-
def collect_concat: [U] () { (
|
|
316
|
-
| () -> ::Enumerator[
|
|
402
|
+
def collect_concat: [U] () { (E) -> (::Array[U] | U) } -> ::Array[U]
|
|
403
|
+
| () -> ::Enumerator[E, ::Array[untyped]]
|
|
317
404
|
|
|
318
405
|
# <!--
|
|
319
406
|
# rdoc-file=enum.c
|
|
@@ -324,7 +411,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
324
411
|
# a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
|
|
325
412
|
# a.compact # => [0, "a", false, false, "a", 0]
|
|
326
413
|
#
|
|
327
|
-
def compact: () -> Array[
|
|
414
|
+
def compact: () -> Array[E]
|
|
328
415
|
|
|
329
416
|
# <!--
|
|
330
417
|
# rdoc-file=enum.c
|
|
@@ -340,8 +427,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
340
427
|
# [0, 1, 2].count # => 3
|
|
341
428
|
# {foo: 0, bar: 1, baz: 2}.count # => 3
|
|
342
429
|
#
|
|
343
|
-
# With argument `object` given, returns the number of elements that are
|
|
344
|
-
# `object`:
|
|
430
|
+
# With argument `object` given, returns the number of elements that are
|
|
431
|
+
# <code>==</code> to `object`:
|
|
345
432
|
#
|
|
346
433
|
# [0, 1, 2, 1].count(1) # => 2
|
|
347
434
|
#
|
|
@@ -352,8 +439,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
352
439
|
# {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2
|
|
353
440
|
#
|
|
354
441
|
def count: () -> Integer
|
|
355
|
-
| (
|
|
356
|
-
| () { (
|
|
442
|
+
| (E) -> Integer
|
|
443
|
+
| () { (E) -> boolish } -> Integer
|
|
357
444
|
|
|
358
445
|
# <!--
|
|
359
446
|
# rdoc-file=enum.c
|
|
@@ -380,8 +467,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
380
467
|
#
|
|
381
468
|
# When no block is given, returns an Enumerator.
|
|
382
469
|
#
|
|
383
|
-
def cycle: (?Integer n) { (
|
|
384
|
-
| (?Integer n) -> ::Enumerator[
|
|
470
|
+
def cycle: (?Integer n) { (E arg0) -> untyped } -> NilClass
|
|
471
|
+
| (?Integer n) -> ::Enumerator[E, NilClass]
|
|
385
472
|
|
|
386
473
|
# <!-- rdoc-file=enum.c -->
|
|
387
474
|
# Returns the first element for which the block returns a truthy value.
|
|
@@ -401,8 +488,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
401
488
|
#
|
|
402
489
|
# With no block given, returns an Enumerator.
|
|
403
490
|
#
|
|
404
|
-
def detect: (?Proc ifnone) { (
|
|
405
|
-
| (?Proc ifnone) -> ::Enumerator[
|
|
491
|
+
def detect: (?Proc ifnone) { (E) -> boolish } -> E?
|
|
492
|
+
| (?Proc ifnone) -> ::Enumerator[E, E?]
|
|
406
493
|
|
|
407
494
|
# <!--
|
|
408
495
|
# rdoc-file=enum.c
|
|
@@ -421,7 +508,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
421
508
|
# h = {foo: 0, bar: 1, baz: 2, bat: 3}
|
|
422
509
|
# h.drop(2) # => [[:baz, 2], [:bat, 3]]
|
|
423
510
|
#
|
|
424
|
-
def drop: (Integer n) -> ::Array[
|
|
511
|
+
def drop: (Integer n) -> ::Array[E]
|
|
425
512
|
|
|
426
513
|
# <!--
|
|
427
514
|
# rdoc-file=enum.c
|
|
@@ -438,8 +525,19 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
438
525
|
#
|
|
439
526
|
# With no block given, returns an Enumerator.
|
|
440
527
|
#
|
|
441
|
-
|
|
442
|
-
|
|
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]]
|
|
443
541
|
|
|
444
542
|
# <!--
|
|
445
543
|
# rdoc-file=enum.c
|
|
@@ -460,16 +558,16 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
460
558
|
#
|
|
461
559
|
# With no block given, returns an Enumerator.
|
|
462
560
|
#
|
|
463
|
-
def each_cons: (Integer n) { (::Array[
|
|
464
|
-
| (Integer n) -> ::Enumerator[::Array[
|
|
561
|
+
def each_cons: (Integer n) { (::Array[E]) -> void } -> self
|
|
562
|
+
| (Integer n) -> ::Enumerator[::Array[E], self]
|
|
465
563
|
|
|
466
564
|
# <!--
|
|
467
565
|
# rdoc-file=enum.c
|
|
468
566
|
# - each_with_index(*args) {|element, i| ..... } -> self
|
|
469
567
|
# - each_with_index(*args) -> enumerator
|
|
470
568
|
# -->
|
|
471
|
-
# Invoke
|
|
472
|
-
# element and its index; returns `self`:
|
|
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`:
|
|
473
571
|
#
|
|
474
572
|
# h = {}
|
|
475
573
|
# (1..4).each_with_index {|element, i| h[element] = i } # => 1..4
|
|
@@ -488,8 +586,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
488
586
|
#
|
|
489
587
|
# With no block given, returns an Enumerator.
|
|
490
588
|
#
|
|
491
|
-
def each_with_index: () { (
|
|
492
|
-
| () -> ::Enumerator[[
|
|
589
|
+
def each_with_index: () { (E, Integer index) -> untyped } -> self
|
|
590
|
+
| () -> ::Enumerator[[ E, Integer ], self]
|
|
493
591
|
|
|
494
592
|
# <!--
|
|
495
593
|
# rdoc-file=enum.c
|
|
@@ -507,18 +605,18 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
507
605
|
#
|
|
508
606
|
# With no block given, returns an Enumerator.
|
|
509
607
|
#
|
|
510
|
-
def each_with_object: [U] (U obj) { (
|
|
511
|
-
| [U] (U obj) -> ::Enumerator[[
|
|
608
|
+
def each_with_object: [U] (U obj) { (E, U obj) -> untyped } -> U
|
|
609
|
+
| [U] (U obj) -> ::Enumerator[[ E, U ], U]
|
|
512
610
|
|
|
513
611
|
# <!-- rdoc-file=enum.c -->
|
|
514
612
|
# Returns an array containing the items in `self`:
|
|
515
613
|
#
|
|
516
614
|
# (0..4).to_a # => [0, 1, 2, 3, 4]
|
|
517
615
|
#
|
|
518
|
-
def entries: () -> ::Array[
|
|
616
|
+
def entries: () -> ::Array[E]
|
|
519
617
|
|
|
520
618
|
def enum_for: (Symbol method, *untyped, **untyped) ?{ (?) -> Integer } -> Enumerator[untyped, untyped]
|
|
521
|
-
| () ?{ () -> Integer } -> Enumerator[
|
|
619
|
+
| () ?{ () -> Integer } -> Enumerator[E, self]
|
|
522
620
|
|
|
523
621
|
%a{annotate:rdoc:skip}
|
|
524
622
|
alias to_enum enum_for
|
|
@@ -541,8 +639,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
541
639
|
#
|
|
542
640
|
# Related: #reject.
|
|
543
641
|
#
|
|
544
|
-
def find_all: () { (
|
|
545
|
-
| () -> ::Enumerator[
|
|
642
|
+
def find_all: () { (E) -> boolish } -> ::Array[E]
|
|
643
|
+
| () -> ::Enumerator[E, ::Array[E]]
|
|
546
644
|
|
|
547
645
|
# <!-- rdoc-file=enum.c -->
|
|
548
646
|
# Returns an array containing elements selected by the block.
|
|
@@ -586,7 +684,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
586
684
|
# `nil` if no such element is found.
|
|
587
685
|
#
|
|
588
686
|
# With argument `object` given, returns the index of the first element that is
|
|
589
|
-
#
|
|
687
|
+
# <code>==</code> `object`:
|
|
590
688
|
#
|
|
591
689
|
# ['a', 'b', 'c', 'b'].find_index('b') # => 1
|
|
592
690
|
#
|
|
@@ -599,8 +697,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
599
697
|
# With no argument and no block given, returns an Enumerator.
|
|
600
698
|
#
|
|
601
699
|
def find_index: (untyped value) -> Integer?
|
|
602
|
-
| () { (
|
|
603
|
-
| () -> ::Enumerator[
|
|
700
|
+
| () { (E) -> boolish } -> Integer?
|
|
701
|
+
| () -> ::Enumerator[E, Integer?]
|
|
604
702
|
|
|
605
703
|
# <!--
|
|
606
704
|
# rdoc-file=enum.c
|
|
@@ -625,8 +723,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
625
723
|
# {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]]
|
|
626
724
|
# [].first(2) # => []
|
|
627
725
|
#
|
|
628
|
-
def first: () ->
|
|
629
|
-
| (_ToInt n) -> ::Array[
|
|
726
|
+
def first: () -> E?
|
|
727
|
+
| (_ToInt n) -> ::Array[E]
|
|
630
728
|
|
|
631
729
|
# <!--
|
|
632
730
|
# rdoc-file=enum.c
|
|
@@ -637,7 +735,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
637
735
|
# pattern.
|
|
638
736
|
#
|
|
639
737
|
# With no block given, returns an array containing each element for which
|
|
640
|
-
#
|
|
738
|
+
# <code>pattern === element</code> is `true`:
|
|
641
739
|
#
|
|
642
740
|
# a = ['foo', 'bar', 'car', 'moo']
|
|
643
741
|
# a.grep(/ar/) # => ["bar", "car"]
|
|
@@ -652,19 +750,19 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
652
750
|
#
|
|
653
751
|
# Related: #grep_v.
|
|
654
752
|
#
|
|
655
|
-
def grep: (untyped arg0) -> ::Array[
|
|
656
|
-
| [U] (untyped arg0) { (
|
|
753
|
+
def grep: (untyped arg0) -> ::Array[E]
|
|
754
|
+
| [U] (untyped arg0) { (E arg0) -> U } -> ::Array[U]
|
|
657
755
|
|
|
658
756
|
# <!--
|
|
659
757
|
# rdoc-file=enum.c
|
|
660
758
|
# - grep_v(pattern) -> array
|
|
661
759
|
# - grep_v(pattern) {|element| ... } -> array
|
|
662
760
|
# -->
|
|
663
|
-
# Returns an array of objects based on elements of `self` that
|
|
664
|
-
# given pattern.
|
|
761
|
+
# Returns an array of objects based on elements of `self` that <em>don't</em>
|
|
762
|
+
# match the given pattern.
|
|
665
763
|
#
|
|
666
764
|
# With no block given, returns an array containing each element for which
|
|
667
|
-
#
|
|
765
|
+
# <code>pattern === element</code> is `false`:
|
|
668
766
|
#
|
|
669
767
|
# a = ['foo', 'bar', 'car', 'moo']
|
|
670
768
|
# a.grep_v(/ar/) # => ["foo", "moo"]
|
|
@@ -679,8 +777,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
679
777
|
#
|
|
680
778
|
# Related: #grep.
|
|
681
779
|
#
|
|
682
|
-
def grep_v: (untyped) -> ::Array[
|
|
683
|
-
| [U] (untyped) { (
|
|
780
|
+
def grep_v: (untyped) -> ::Array[E]
|
|
781
|
+
| [U] (untyped) { (E) -> U } -> ::Array[U]
|
|
684
782
|
|
|
685
783
|
# <!--
|
|
686
784
|
# rdoc-file=enum.c
|
|
@@ -703,11 +801,11 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
703
801
|
#
|
|
704
802
|
# With no block given, returns an Enumerator.
|
|
705
803
|
#
|
|
706
|
-
def group_by: [U] () { (
|
|
707
|
-
| () -> ::Enumerator[
|
|
804
|
+
def group_by: [U] () { (E arg0) -> U } -> ::Hash[U, ::Array[E]]
|
|
805
|
+
| () -> ::Enumerator[E, ::Array[E]]
|
|
708
806
|
|
|
709
807
|
# <!-- rdoc-file=enum.c -->
|
|
710
|
-
# Returns whether for any element
|
|
808
|
+
# Returns whether for any element <code>object == element</code>:
|
|
711
809
|
#
|
|
712
810
|
# (1..4).include?(2) # => true
|
|
713
811
|
# (1..4).include?(5) # => false
|
|
@@ -718,7 +816,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
718
816
|
# {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
|
|
719
817
|
# {foo: 0, bar: 1, baz: 2}.include?(0) # => false
|
|
720
818
|
#
|
|
721
|
-
def include?: (
|
|
819
|
+
def include?: (top arg0) -> bool
|
|
722
820
|
|
|
723
821
|
# <!--
|
|
724
822
|
# rdoc-file=enum.c
|
|
@@ -765,13 +863,13 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
765
863
|
# product #=> 24
|
|
766
864
|
#
|
|
767
865
|
# When this runs, the block is first called with `1` (the initial value) and `2`
|
|
768
|
-
# (the first element of the array). The block returns
|
|
769
|
-
# iteration the block is called with `2` (the previous result) and `3`.
|
|
770
|
-
# block returns `6`, and is called one last time with `6` and `4`. The
|
|
771
|
-
# the block, `24` becomes the value returned by `inject`. This code
|
|
772
|
-
# product of the elements in the enumerable.
|
|
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.
|
|
773
871
|
#
|
|
774
|
-
#
|
|
872
|
+
# <strong>First Shortcut: Default Initial value</strong>
|
|
775
873
|
#
|
|
776
874
|
# In the case of the previous example, the initial value, `1`, wasn't really
|
|
777
875
|
# necessary: the calculation of the product of a list of numbers is
|
|
@@ -808,7 +906,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
808
906
|
# Note that the last line of the block is just the word `counts`. This ensures
|
|
809
907
|
# the return value of the block is the result that's being calculated.
|
|
810
908
|
#
|
|
811
|
-
#
|
|
909
|
+
# <strong>Second Shortcut: a Reducer function</strong>
|
|
812
910
|
#
|
|
813
911
|
# A *reducer function* is a function that takes a partial result and the next
|
|
814
912
|
# value, returning the next partial result. The block that is given to `inject`
|
|
@@ -853,11 +951,11 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
853
951
|
# position = "nnneesw".chars.reduce(Turtle.new, :move)
|
|
854
952
|
# position #=>> #<Turtle:0x00000001052f4698 @y=2, @x=1>
|
|
855
953
|
#
|
|
856
|
-
#
|
|
954
|
+
# <strong>Third Shortcut: Reducer With no Initial Value</strong>
|
|
857
955
|
#
|
|
858
956
|
# If your reducer returns a value that it can accept as a parameter, then you
|
|
859
|
-
# don't have to pass in an initial value. Here
|
|
860
|
-
# function:
|
|
957
|
+
# don't have to pass in an initial value. Here <code>:*</code> is the name of
|
|
958
|
+
# the *times* function:
|
|
861
959
|
#
|
|
862
960
|
# product = [ 2, 3, 4 ].inject(:*)
|
|
863
961
|
# product # => 24
|
|
@@ -874,8 +972,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
874
972
|
#
|
|
875
973
|
def inject: (untyped init, Symbol method) -> untyped
|
|
876
974
|
| (Symbol method) -> untyped
|
|
877
|
-
| [A] (A initial) { (A,
|
|
878
|
-
| () { (
|
|
975
|
+
| [A] (A initial) { (A, E) -> A } -> A
|
|
976
|
+
| () { (E, E) -> E } -> E
|
|
879
977
|
|
|
880
978
|
# <!--
|
|
881
979
|
# rdoc-file=enum.c
|
|
@@ -888,7 +986,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
888
986
|
# The ordering of equal elements is indeterminate and may be unstable.
|
|
889
987
|
#
|
|
890
988
|
# With no argument and no block, returns the maximum element, using the
|
|
891
|
-
# elements' own method
|
|
989
|
+
# elements' own method <code>#<=></code> for comparison:
|
|
892
990
|
#
|
|
893
991
|
# (1..4).max # => 4
|
|
894
992
|
# (-4..-1).max # => -1
|
|
@@ -908,9 +1006,9 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
908
1006
|
# With a block given, the block determines the maximum elements. The block is
|
|
909
1007
|
# called with two elements `a` and `b`, and must return:
|
|
910
1008
|
#
|
|
911
|
-
# * A negative integer if
|
|
912
|
-
# * Zero if
|
|
913
|
-
# * A positive integer if
|
|
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>.
|
|
914
1012
|
#
|
|
915
1013
|
# With a block given and no argument, returns the maximum element as determined
|
|
916
1014
|
# by the block:
|
|
@@ -932,10 +1030,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
932
1030
|
#
|
|
933
1031
|
# Related: #min, #minmax, #max_by.
|
|
934
1032
|
#
|
|
935
|
-
def max: () ->
|
|
936
|
-
| () { (
|
|
937
|
-
| (Integer arg0) -> ::Array[
|
|
938
|
-
| (Integer arg0) { (
|
|
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]
|
|
939
1037
|
|
|
940
1038
|
# <!--
|
|
941
1039
|
# rdoc-file=enum.c
|
|
@@ -970,10 +1068,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
970
1068
|
#
|
|
971
1069
|
# Related: #max, #minmax, #min_by.
|
|
972
1070
|
#
|
|
973
|
-
def max_by: () -> ::Enumerator[
|
|
974
|
-
| () { (
|
|
975
|
-
| (Integer arg0) -> ::Enumerator[
|
|
976
|
-
| (Integer arg0) { (
|
|
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]
|
|
977
1075
|
|
|
978
1076
|
# <!--
|
|
979
1077
|
# rdoc-file=enum.c
|
|
@@ -986,7 +1084,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
986
1084
|
# The ordering of equal elements is indeterminate and may be unstable.
|
|
987
1085
|
#
|
|
988
1086
|
# With no argument and no block, returns the minimum element, using the
|
|
989
|
-
# elements' own method
|
|
1087
|
+
# elements' own method <code>#<=></code> for comparison:
|
|
990
1088
|
#
|
|
991
1089
|
# (1..4).min # => 1
|
|
992
1090
|
# (-4..-1).min # => -4
|
|
@@ -1006,9 +1104,9 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1006
1104
|
# With a block given, the block determines the minimum elements. The block is
|
|
1007
1105
|
# called with two elements `a` and `b`, and must return:
|
|
1008
1106
|
#
|
|
1009
|
-
# * A negative integer if
|
|
1010
|
-
# * Zero if
|
|
1011
|
-
# * A positive integer if
|
|
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>.
|
|
1012
1110
|
#
|
|
1013
1111
|
# With a block given and no argument, returns the minimum element as determined
|
|
1014
1112
|
# by the block:
|
|
@@ -1030,10 +1128,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1030
1128
|
#
|
|
1031
1129
|
# Related: #min_by, #minmax, #max.
|
|
1032
1130
|
#
|
|
1033
|
-
def min: () ->
|
|
1034
|
-
| () { (
|
|
1035
|
-
| (Integer arg0) -> ::Array[
|
|
1036
|
-
| (Integer arg0) { (
|
|
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]
|
|
1037
1135
|
|
|
1038
1136
|
# <!--
|
|
1039
1137
|
# rdoc-file=enum.c
|
|
@@ -1068,10 +1166,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1068
1166
|
#
|
|
1069
1167
|
# Related: #min, #minmax, #max_by.
|
|
1070
1168
|
#
|
|
1071
|
-
def min_by: () -> ::Enumerator[
|
|
1072
|
-
| () { (
|
|
1073
|
-
| (Integer arg0) -> ::Enumerator[
|
|
1074
|
-
| (Integer arg0) { (
|
|
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]
|
|
1075
1173
|
|
|
1076
1174
|
# <!--
|
|
1077
1175
|
# rdoc-file=enum.c
|
|
@@ -1083,7 +1181,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1083
1181
|
# indeterminate and may be unstable.
|
|
1084
1182
|
#
|
|
1085
1183
|
# With no argument and no block, returns the minimum and maximum elements, using
|
|
1086
|
-
# the elements' own method
|
|
1184
|
+
# the elements' own method <code>#<=></code> for comparison:
|
|
1087
1185
|
#
|
|
1088
1186
|
# (1..4).minmax # => [1, 4]
|
|
1089
1187
|
# (-4..-1).minmax # => [-4, -1]
|
|
@@ -1102,8 +1200,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1102
1200
|
#
|
|
1103
1201
|
# Related: #min, #max, #minmax_by.
|
|
1104
1202
|
#
|
|
1105
|
-
def minmax: () -> [
|
|
1106
|
-
| () { (
|
|
1203
|
+
def minmax: () -> [ E?, E? ]
|
|
1204
|
+
| () { (E arg0, E arg1) -> Integer } -> [ E?, E? ]
|
|
1107
1205
|
|
|
1108
1206
|
# <!--
|
|
1109
1207
|
# rdoc-file=enum.c
|
|
@@ -1126,8 +1224,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1126
1224
|
#
|
|
1127
1225
|
# Related: #max_by, #minmax, #min_by.
|
|
1128
1226
|
#
|
|
1129
|
-
def minmax_by: () -> [
|
|
1130
|
-
| () { (
|
|
1227
|
+
def minmax_by: () -> [ E?, E? ]
|
|
1228
|
+
| () { (E arg0) -> (Comparable | ::Array[untyped]) } -> [ E?, E? ]
|
|
1131
1229
|
|
|
1132
1230
|
# <!--
|
|
1133
1231
|
# rdoc-file=enum.c
|
|
@@ -1146,7 +1244,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1146
1244
|
# [].none? # => true
|
|
1147
1245
|
#
|
|
1148
1246
|
# With argument `pattern` and no block, returns whether for no element
|
|
1149
|
-
# `element`,
|
|
1247
|
+
# `element`, <code>pattern === element</code>:
|
|
1150
1248
|
#
|
|
1151
1249
|
# [nil, false, 1.1].none?(Integer) # => true
|
|
1152
1250
|
# %w[bar baz bat bam].none?(/m/) # => false
|
|
@@ -1167,7 +1265,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1167
1265
|
# Related: #one?, #all?, #any?.
|
|
1168
1266
|
#
|
|
1169
1267
|
def none?: () -> bool
|
|
1170
|
-
| ()
|
|
1268
|
+
| (_Pattern) -> bool
|
|
1269
|
+
| () { (E) -> boolish } -> bool
|
|
1171
1270
|
|
|
1172
1271
|
# <!--
|
|
1173
1272
|
# rdoc-file=enum.c
|
|
@@ -1187,7 +1286,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1187
1286
|
# [].one? # => false
|
|
1188
1287
|
#
|
|
1189
1288
|
# With argument `pattern` and no block, returns whether for exactly one element
|
|
1190
|
-
# `element`,
|
|
1289
|
+
# `element`, <code>pattern === element</code>:
|
|
1191
1290
|
#
|
|
1192
1291
|
# [nil, false, 0].one?(Integer) # => true
|
|
1193
1292
|
# [nil, false, 0].one?(Numeric) # => true
|
|
@@ -1210,7 +1309,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1210
1309
|
# Related: #none?, #all?, #any?.
|
|
1211
1310
|
#
|
|
1212
1311
|
def one?: () -> bool
|
|
1213
|
-
| ()
|
|
1312
|
+
| (_Pattern) -> bool
|
|
1313
|
+
| () { (E) -> boolish } -> bool
|
|
1214
1314
|
|
|
1215
1315
|
# <!--
|
|
1216
1316
|
# rdoc-file=enum.c
|
|
@@ -1239,8 +1339,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1239
1339
|
#
|
|
1240
1340
|
# Related: Enumerable#group_by.
|
|
1241
1341
|
#
|
|
1242
|
-
def partition: () { (
|
|
1243
|
-
| () -> ::Enumerator[
|
|
1342
|
+
def partition: () { (E) -> boolish } -> [ ::Array[E], ::Array[E] ]
|
|
1343
|
+
| () -> ::Enumerator[E, [ ::Array[E], ::Array[E] ]]
|
|
1244
1344
|
|
|
1245
1345
|
# <!--
|
|
1246
1346
|
# rdoc-file=enum.c
|
|
@@ -1259,8 +1359,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1259
1359
|
#
|
|
1260
1360
|
# Related: #select.
|
|
1261
1361
|
#
|
|
1262
|
-
def reject: () { (
|
|
1263
|
-
| () -> ::Enumerator[
|
|
1362
|
+
def reject: () { (E) -> boolish } -> ::Array[E]
|
|
1363
|
+
| () -> ::Enumerator[E, ::Array[E]]
|
|
1264
1364
|
|
|
1265
1365
|
# <!--
|
|
1266
1366
|
# rdoc-file=enum.c
|
|
@@ -1286,8 +1386,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1286
1386
|
#
|
|
1287
1387
|
# With no block given, returns an Enumerator.
|
|
1288
1388
|
#
|
|
1289
|
-
def reverse_each: () { (
|
|
1290
|
-
| () -> ::Enumerator[
|
|
1389
|
+
def reverse_each: () { (E arg0) -> untyped } -> void
|
|
1390
|
+
| () -> ::Enumerator[E]
|
|
1291
1391
|
|
|
1292
1392
|
# <!--
|
|
1293
1393
|
# rdoc-file=enum.c
|
|
@@ -1297,7 +1397,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1297
1397
|
# Returns an array containing the sorted elements of `self`. The ordering of
|
|
1298
1398
|
# equal elements is indeterminate and may be unstable.
|
|
1299
1399
|
#
|
|
1300
|
-
# With no block given, the sort compares using the elements' own method
|
|
1400
|
+
# With no block given, the sort compares using the elements' own method
|
|
1401
|
+
# <code>#<=></code>:
|
|
1301
1402
|
#
|
|
1302
1403
|
# %w[b c a d].sort # => ["a", "b", "c", "d"]
|
|
1303
1404
|
# {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
|
|
@@ -1305,9 +1406,9 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1305
1406
|
# With a block given, comparisons in the block determine the ordering. The block
|
|
1306
1407
|
# is called with two elements `a` and `b`, and must return:
|
|
1307
1408
|
#
|
|
1308
|
-
# * A negative integer if
|
|
1309
|
-
# * Zero if
|
|
1310
|
-
# * A positive integer if
|
|
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>.
|
|
1311
1412
|
#
|
|
1312
1413
|
# Examples:
|
|
1313
1414
|
#
|
|
@@ -1319,8 +1420,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1319
1420
|
# See also #sort_by. It implements a Schwartzian transform which is useful when
|
|
1320
1421
|
# key computation or comparison is expensive.
|
|
1321
1422
|
#
|
|
1322
|
-
def sort: () -> ::Array[
|
|
1323
|
-
| () { (
|
|
1423
|
+
def sort: () -> ::Array[E]
|
|
1424
|
+
| () { (E arg0, E arg1) -> Integer } -> ::Array[E]
|
|
1324
1425
|
|
|
1325
1426
|
# <!--
|
|
1326
1427
|
# rdoc-file=enum.c
|
|
@@ -1355,7 +1456,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1355
1456
|
# b.report("Sort by") { a.sort_by { |a| a } }
|
|
1356
1457
|
# end
|
|
1357
1458
|
#
|
|
1358
|
-
#
|
|
1459
|
+
# <em>produces:</em>
|
|
1359
1460
|
#
|
|
1360
1461
|
# user system total real
|
|
1361
1462
|
# Sort 0.180000 0.000000 0.180000 ( 0.175469)
|
|
@@ -1400,8 +1501,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1400
1501
|
#
|
|
1401
1502
|
# ary.sort_by { ... }.reverse!
|
|
1402
1503
|
#
|
|
1403
|
-
def sort_by: () { (
|
|
1404
|
-
| () -> ::Enumerator[
|
|
1504
|
+
def sort_by: () { (E arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[E]
|
|
1505
|
+
| () -> ::Enumerator[E, ::Array[E]]
|
|
1405
1506
|
|
|
1406
1507
|
# <!--
|
|
1407
1508
|
# rdoc-file=enum.c
|
|
@@ -1416,7 +1517,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1416
1517
|
# h = {foo: 0, bar: 1, baz: 2, bat: 3}
|
|
1417
1518
|
# h.take(2) # => [[:foo, 0], [:bar, 1]]
|
|
1418
1519
|
#
|
|
1419
|
-
def take: (Integer n) -> ::Array[
|
|
1520
|
+
def take: (Integer n) -> ::Array[E]
|
|
1420
1521
|
|
|
1421
1522
|
# <!--
|
|
1422
1523
|
# rdoc-file=enum.c
|
|
@@ -1433,8 +1534,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1433
1534
|
#
|
|
1434
1535
|
# With no block given, returns an Enumerator.
|
|
1435
1536
|
#
|
|
1436
|
-
def take_while: () { (
|
|
1437
|
-
| () -> ::Enumerator[
|
|
1537
|
+
def take_while: () { (E) -> boolish } -> ::Array[E]
|
|
1538
|
+
| () -> ::Enumerator[E, ::Array[E]]
|
|
1438
1539
|
|
|
1439
1540
|
# <!--
|
|
1440
1541
|
# rdoc-file=enum.c
|
|
@@ -1456,7 +1557,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1456
1557
|
# block is not passed.
|
|
1457
1558
|
#
|
|
1458
1559
|
def to_h: () -> ::Hash[untyped, untyped]
|
|
1459
|
-
| [T, U] () { (
|
|
1560
|
+
| [T, U] () { (E) -> [ T, U ] } -> ::Hash[T, U]
|
|
1460
1561
|
|
|
1461
1562
|
# <!--
|
|
1462
1563
|
# rdoc-file=enum.c
|
|
@@ -1477,8 +1578,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1477
1578
|
#
|
|
1478
1579
|
# With no block given, returns an Enumerator.
|
|
1479
1580
|
#
|
|
1480
|
-
def each_slice: (Integer n) { (::Array[
|
|
1481
|
-
| (Integer n) -> ::Enumerator[::Array[
|
|
1581
|
+
def each_slice: (Integer n) { (::Array[E]) -> void } -> self
|
|
1582
|
+
| (Integer n) -> ::Enumerator[::Array[E], self]
|
|
1482
1583
|
|
|
1483
1584
|
interface _NotFound[T]
|
|
1484
1585
|
def call: () -> T
|
|
@@ -1506,10 +1607,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1506
1607
|
#
|
|
1507
1608
|
# With no block given, returns an Enumerator.
|
|
1508
1609
|
#
|
|
1509
|
-
def find: () { (
|
|
1510
|
-
| () -> ::Enumerator[
|
|
1511
|
-
| [T] (_NotFound[T] ifnone) { (
|
|
1512
|
-
| [T] (_NotFound[T] ifnone) -> ::Enumerator[
|
|
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]
|
|
1513
1614
|
|
|
1514
1615
|
# <!--
|
|
1515
1616
|
# rdoc-file=enum.c
|
|
@@ -1530,8 +1631,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1530
1631
|
#
|
|
1531
1632
|
# Alias: #collect_concat.
|
|
1532
1633
|
#
|
|
1533
|
-
def flat_map: [U] () { (
|
|
1534
|
-
| () -> ::Enumerator[
|
|
1634
|
+
def flat_map: [U] () { (E) -> (Array[U] | U) } -> Array[U]
|
|
1635
|
+
| () -> ::Enumerator[E, Array[untyped]]
|
|
1535
1636
|
|
|
1536
1637
|
# <!-- rdoc-file=enum.c -->
|
|
1537
1638
|
# Returns an array of objects returned by the block.
|
|
@@ -1544,14 +1645,14 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1544
1645
|
#
|
|
1545
1646
|
# With no block given, returns an Enumerator.
|
|
1546
1647
|
#
|
|
1547
|
-
def map: [U] () { (
|
|
1548
|
-
| () -> ::Enumerator[
|
|
1648
|
+
def map: [U] () { (E arg0) -> U } -> ::Array[U]
|
|
1649
|
+
| () -> ::Enumerator[E, ::Array[untyped]]
|
|
1549
1650
|
|
|
1550
1651
|
# <!--
|
|
1551
1652
|
# rdoc-file=enum.c
|
|
1552
1653
|
# - include?(object) -> true or false
|
|
1553
1654
|
# -->
|
|
1554
|
-
# Returns whether for any element
|
|
1655
|
+
# Returns whether for any element <code>object == element</code>:
|
|
1555
1656
|
#
|
|
1556
1657
|
# (1..4).include?(2) # => true
|
|
1557
1658
|
# (1..4).include?(5) # => false
|
|
@@ -1562,7 +1663,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1562
1663
|
# {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
|
|
1563
1664
|
# {foo: 0, bar: 1, baz: 2}.include?(0) # => false
|
|
1564
1665
|
#
|
|
1565
|
-
def member?: (
|
|
1666
|
+
def member?: (top arg0) -> bool
|
|
1566
1667
|
|
|
1567
1668
|
# <!-- rdoc-file=enum.c -->
|
|
1568
1669
|
# Returns the result of applying a reducer to an initial value and the first
|
|
@@ -1603,13 +1704,13 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1603
1704
|
# product #=> 24
|
|
1604
1705
|
#
|
|
1605
1706
|
# When this runs, the block is first called with `1` (the initial value) and `2`
|
|
1606
|
-
# (the first element of the array). The block returns
|
|
1607
|
-
# iteration the block is called with `2` (the previous result) and `3`.
|
|
1608
|
-
# block returns `6`, and is called one last time with `6` and `4`. The
|
|
1609
|
-
# the block, `24` becomes the value returned by `inject`. This code
|
|
1610
|
-
# product of the elements in the enumerable.
|
|
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.
|
|
1611
1712
|
#
|
|
1612
|
-
#
|
|
1713
|
+
# <strong>First Shortcut: Default Initial value</strong>
|
|
1613
1714
|
#
|
|
1614
1715
|
# In the case of the previous example, the initial value, `1`, wasn't really
|
|
1615
1716
|
# necessary: the calculation of the product of a list of numbers is
|
|
@@ -1646,7 +1747,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1646
1747
|
# Note that the last line of the block is just the word `counts`. This ensures
|
|
1647
1748
|
# the return value of the block is the result that's being calculated.
|
|
1648
1749
|
#
|
|
1649
|
-
#
|
|
1750
|
+
# <strong>Second Shortcut: a Reducer function</strong>
|
|
1650
1751
|
#
|
|
1651
1752
|
# A *reducer function* is a function that takes a partial result and the next
|
|
1652
1753
|
# value, returning the next partial result. The block that is given to `inject`
|
|
@@ -1691,11 +1792,11 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1691
1792
|
# position = "nnneesw".chars.reduce(Turtle.new, :move)
|
|
1692
1793
|
# position #=>> #<Turtle:0x00000001052f4698 @y=2, @x=1>
|
|
1693
1794
|
#
|
|
1694
|
-
#
|
|
1795
|
+
# <strong>Third Shortcut: Reducer With no Initial Value</strong>
|
|
1695
1796
|
#
|
|
1696
1797
|
# If your reducer returns a value that it can accept as a parameter, then you
|
|
1697
|
-
# don't have to pass in an initial value. Here
|
|
1698
|
-
# function:
|
|
1798
|
+
# don't have to pass in an initial value. Here <code>:*</code> is the name of
|
|
1799
|
+
# the *times* function:
|
|
1699
1800
|
#
|
|
1700
1801
|
# product = [ 2, 3, 4 ].inject(:*)
|
|
1701
1802
|
# product # => 24
|
|
@@ -1720,7 +1821,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1720
1821
|
#
|
|
1721
1822
|
# (0..4).to_a # => [0, 1, 2, 3, 4]
|
|
1722
1823
|
#
|
|
1723
|
-
def to_a: () -> ::Array[
|
|
1824
|
+
def to_a: () -> ::Array[E]
|
|
1724
1825
|
|
|
1725
1826
|
# <!--
|
|
1726
1827
|
# rdoc-file=enumerator.c
|
|
@@ -1750,7 +1851,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1750
1851
|
# # show pythagorean triples less than 100
|
|
1751
1852
|
# p pythagorean_triples.take_while { |*, z| z < 100 }.force
|
|
1752
1853
|
#
|
|
1753
|
-
def lazy: () -> Enumerator::Lazy[
|
|
1854
|
+
def lazy: () -> Enumerator::Lazy[E]
|
|
1754
1855
|
|
|
1755
1856
|
# <!--
|
|
1756
1857
|
# rdoc-file=enum.c
|
|
@@ -1758,7 +1859,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1758
1859
|
# - uniq {|element| ... } -> array
|
|
1759
1860
|
# -->
|
|
1760
1861
|
# With no block, returns a new array containing only unique elements; the array
|
|
1761
|
-
# has no two elements `e0` and `e1` such that
|
|
1862
|
+
# has no two elements `e0` and `e1` such that <code>e0.eql?(e1)</code>:
|
|
1762
1863
|
#
|
|
1763
1864
|
# %w[a b c c b a a b c].uniq # => ["a", "b", "c"]
|
|
1764
1865
|
# [0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]
|
|
@@ -1771,8 +1872,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1771
1872
|
# a = %w[a b c d e e d c b a a b c d e]
|
|
1772
1873
|
# a.uniq {|c| c < 'c' } # => ["a", "c"]
|
|
1773
1874
|
#
|
|
1774
|
-
def uniq: () -> ::Array[
|
|
1775
|
-
| () { (
|
|
1875
|
+
def uniq: () -> ::Array[E]
|
|
1876
|
+
| () { (E item) -> untyped } -> ::Array[E]
|
|
1776
1877
|
|
|
1777
1878
|
# <!--
|
|
1778
1879
|
# rdoc-file=enum.c
|
|
@@ -1785,12 +1886,12 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1785
1886
|
# (1..100).sum(1) # => 5051
|
|
1786
1887
|
# ('a'..'d').sum('foo') # => "fooabcd"
|
|
1787
1888
|
#
|
|
1788
|
-
# Generally, the sum is computed using methods
|
|
1789
|
-
# optimizations, those methods may not be used, and so any
|
|
1790
|
-
# methods may not have effect here.
|
|
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.
|
|
1791
1892
|
#
|
|
1792
1893
|
# One such optimization: When possible, computes using Gauss's summation formula
|
|
1793
|
-
#
|
|
1894
|
+
# <em>n(n+1)/2</em>:
|
|
1794
1895
|
#
|
|
1795
1896
|
# 100 * (100 + 1) / 2 # => 5050
|
|
1796
1897
|
#
|
|
@@ -1803,10 +1904,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1803
1904
|
# h.sum {|key, value| value.odd? ? value : 0 } # => 9
|
|
1804
1905
|
# ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
|
|
1805
1906
|
#
|
|
1806
|
-
def sum: () -> (
|
|
1807
|
-
| [T] () { (
|
|
1808
|
-
| [T] (?T arg0) -> (
|
|
1809
|
-
| [U] (?U arg0) { (
|
|
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
|
|
1810
1911
|
|
|
1811
1912
|
# <!--
|
|
1812
1913
|
# rdoc-file=enum.c
|
|
@@ -1823,8 +1924,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1823
1924
|
#
|
|
1824
1925
|
# When no block given, returns an Enumerator.
|
|
1825
1926
|
#
|
|
1826
|
-
def filter_map: [U] () { (
|
|
1827
|
-
| () -> ::Enumerator[
|
|
1927
|
+
def filter_map: [U] () { (E elem) -> (nil | false | U) } -> ::Array[U]
|
|
1928
|
+
| () -> ::Enumerator[E, ::Array[untyped]]
|
|
1828
1929
|
|
|
1829
1930
|
# <!--
|
|
1830
1931
|
# rdoc-file=enumerator.c
|
|
@@ -1836,7 +1937,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1836
1937
|
# e = (1..3).chain([4, 5])
|
|
1837
1938
|
# e.to_a #=> [1, 2, 3, 4, 5]
|
|
1838
1939
|
#
|
|
1839
|
-
def chain: [Elem2] (*_Each[Elem2] enumerables) -> ::Enumerator::Chain[
|
|
1940
|
+
def chain: [Elem2] (*_Each[Elem2] enumerables) -> ::Enumerator::Chain[E | Elem2]
|
|
1840
1941
|
|
|
1841
1942
|
# <!--
|
|
1842
1943
|
# rdoc-file=enum.c
|
|
@@ -1882,7 +1983,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1882
1983
|
# {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1}
|
|
1883
1984
|
# {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2}
|
|
1884
1985
|
#
|
|
1885
|
-
def tally: (?Hash[
|
|
1986
|
+
def tally: (?Hash[E, Integer] hash) -> ::Hash[E, Integer]
|
|
1886
1987
|
|
|
1887
1988
|
# <!--
|
|
1888
1989
|
# rdoc-file=enum.c
|
|
@@ -1920,8 +2021,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1920
2021
|
#
|
|
1921
2022
|
# With no block given, returns an Enumerator.
|
|
1922
2023
|
#
|
|
1923
|
-
def each_entry: () -> ::Enumerator[
|
|
1924
|
-
| () { (
|
|
2024
|
+
def each_entry: () -> ::Enumerator[E, self]
|
|
2025
|
+
| () { (E arg0) -> untyped } -> self
|
|
1925
2026
|
|
|
1926
2027
|
# <!--
|
|
1927
2028
|
# rdoc-file=enum.c
|
|
@@ -1929,8 +2030,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1929
2030
|
# - zip(*other_enums) {|array| ... } -> nil
|
|
1930
2031
|
# -->
|
|
1931
2032
|
# With no block given, returns a new array `new_array` of size self.size whose
|
|
1932
|
-
# elements are arrays. Each nested array
|
|
1933
|
-
#
|
|
2033
|
+
# elements are arrays. Each nested array <code>new_array[n]</code> is of size
|
|
2034
|
+
# <code>other_enums.size+1</code>, and contains:
|
|
1934
2035
|
#
|
|
1935
2036
|
# * The `n`-th element of self.
|
|
1936
2037
|
# * The `n`-th element of each of the `other_enums`.
|
|
@@ -1954,8 +2055,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1954
2055
|
# # [[:baz, 2], [:gaz, 5], [:haz, 8]]
|
|
1955
2056
|
# # ]
|
|
1956
2057
|
#
|
|
1957
|
-
# If any enumerable in other_enums is smaller than self, fills to
|
|
1958
|
-
# with `nil`:
|
|
2058
|
+
# If any enumerable in other_enums is smaller than self, fills to
|
|
2059
|
+
# <code>self.size</code> with `nil`:
|
|
1959
2060
|
#
|
|
1960
2061
|
# a = [:a0, :a1, :a2, :a3]
|
|
1961
2062
|
# b = [:b0, :b1, :b2]
|
|
@@ -1987,9 +2088,9 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
1987
2088
|
# [:a2, :b2, :c2]
|
|
1988
2089
|
# [:a3, :b3, :c3]
|
|
1989
2090
|
#
|
|
1990
|
-
def zip: [Elem2] (_Each[Elem2] enum) -> Array[[
|
|
2091
|
+
def zip: [Elem2] (_Each[Elem2] enum) -> Array[[ E, Elem2? ]]
|
|
1991
2092
|
| (_Each[untyped], *_Each[untyped]) -> Array[Array[untyped]]
|
|
1992
|
-
| [Elem2] (_Each[Elem2]) { ([
|
|
2093
|
+
| [Elem2] (_Each[Elem2]) { ([ E, Elem2? ]) -> void } -> nil
|
|
1993
2094
|
| (_Each[untyped], *_Each[untyped]) { (Array[untyped]) -> void } -> nil
|
|
1994
2095
|
|
|
1995
2096
|
# <!--
|
|
@@ -2039,8 +2140,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2039
2140
|
# ["E", 8736]
|
|
2040
2141
|
# ["F", 6860]
|
|
2041
2142
|
#
|
|
2042
|
-
# You can use the special symbol
|
|
2043
|
-
# separate
|
|
2143
|
+
# You can use the special symbol <code>:_alone</code> to force an element into
|
|
2144
|
+
# its own separate chunk:
|
|
2044
2145
|
#
|
|
2045
2146
|
# a = [0, 0, 1, 1]
|
|
2046
2147
|
# e = a.chunk{|i| i.even? ? :_alone : true }
|
|
@@ -2055,8 +2156,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2055
2156
|
# }
|
|
2056
2157
|
# }
|
|
2057
2158
|
#
|
|
2058
|
-
# You can use the special symbol
|
|
2059
|
-
# be ignored (not included in any chunk):
|
|
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):
|
|
2060
2161
|
#
|
|
2061
2162
|
# a = [0, 0, -1, 1, 1]
|
|
2062
2163
|
# e = a.chunk{|i| i < 0 ? :_separator : true }
|
|
@@ -2096,8 +2197,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2096
2197
|
# pp lines
|
|
2097
2198
|
# }
|
|
2098
2199
|
#
|
|
2099
|
-
def chunk: [U] () { (
|
|
2100
|
-
| () -> ::Enumerator[
|
|
2200
|
+
def chunk: [U] () { (E elt) -> U } -> ::Enumerator[[ U, ::Array[E] ]]
|
|
2201
|
+
| () -> ::Enumerator[E, ::Enumerator[[ untyped, ::Array[E] ]]]
|
|
2101
2202
|
|
|
2102
2203
|
# <!--
|
|
2103
2204
|
# rdoc-file=enum.c
|
|
@@ -2146,7 +2247,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2146
2247
|
# Enumerable#slice_when does the same, except splitting when the block returns
|
|
2147
2248
|
# `true` instead of `false`.
|
|
2148
2249
|
#
|
|
2149
|
-
def chunk_while: () { (
|
|
2250
|
+
def chunk_while: () { (E elt_before, E elt_after) -> boolish } -> ::Enumerator[::Array[E]]
|
|
2150
2251
|
|
|
2151
2252
|
# <!--
|
|
2152
2253
|
# rdoc-file=enum.c
|
|
@@ -2208,7 +2309,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2208
2309
|
# Enumerable#chunk_while does the same, except splitting when the block returns
|
|
2209
2310
|
# `false` instead of `true`.
|
|
2210
2311
|
#
|
|
2211
|
-
def slice_when: () { (
|
|
2312
|
+
def slice_when: () { (E elt_before, E elt_after) -> boolish } -> ::Enumerator[::Array[E]]
|
|
2212
2313
|
|
|
2213
2314
|
# <!--
|
|
2214
2315
|
# rdoc-file=enum.c
|
|
@@ -2218,11 +2319,11 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2218
2319
|
# Creates an enumerator for each chunked elements. The ends of chunks are
|
|
2219
2320
|
# defined by *pattern* and the block.
|
|
2220
2321
|
#
|
|
2221
|
-
# If
|
|
2222
|
-
# element, the element is end of a chunk.
|
|
2322
|
+
# If <code>_pattern_ === _elt_</code> returns `true` or the block returns `true`
|
|
2323
|
+
# for the element, the element is end of a chunk.
|
|
2223
2324
|
#
|
|
2224
|
-
# The
|
|
2225
|
-
# *enum*.
|
|
2325
|
+
# The <code>===</code> and *block* is called from the first element to the last
|
|
2326
|
+
# element of *enum*.
|
|
2226
2327
|
#
|
|
2227
2328
|
# The result enumerator yields the chunked elements as an array. So `each`
|
|
2228
2329
|
# method can be called as follows:
|
|
@@ -2243,8 +2344,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2243
2344
|
# p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
|
|
2244
2345
|
# #=>["foo\n", "barbaz\n", "\n", "qux\n"]
|
|
2245
2346
|
#
|
|
2246
|
-
def slice_after: (untyped pattern) -> ::Enumerator[::Array[
|
|
2247
|
-
| () { (
|
|
2347
|
+
def slice_after: (untyped pattern) -> ::Enumerator[::Array[E]]
|
|
2348
|
+
| () { (E elt) -> boolish } -> ::Enumerator[::Array[E]]
|
|
2248
2349
|
|
|
2249
2350
|
# <!--
|
|
2250
2351
|
# rdoc-file=enum.c
|
|
@@ -2253,7 +2354,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2253
2354
|
# -->
|
|
2254
2355
|
# With argument `pattern`, returns an enumerator that uses the pattern to
|
|
2255
2356
|
# partition elements into arrays ("slices"). An element begins a new slice if
|
|
2256
|
-
#
|
|
2357
|
+
# <code>element === pattern</code> (or if it is the first element).
|
|
2257
2358
|
#
|
|
2258
2359
|
# a = %w[foo bar fop for baz fob fog bam foy]
|
|
2259
2360
|
# e = a.slice_before(/ba/) # => #<Enumerator: ...>
|
|
@@ -2400,6 +2501,6 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
|
|
|
2400
2501
|
# }
|
|
2401
2502
|
# }
|
|
2402
2503
|
#
|
|
2403
|
-
def slice_before: (untyped pattern) -> ::Enumerator[::Array[
|
|
2404
|
-
| () { (
|
|
2504
|
+
def slice_before: (untyped pattern) -> ::Enumerator[::Array[E]]
|
|
2505
|
+
| () { (E elt) -> boolish } -> ::Enumerator[::Array[E]]
|
|
2405
2506
|
end
|