steep 0.13.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +28 -0
  4. data/Rakefile +0 -13
  5. data/bin/setup +0 -2
  6. data/bin/smoke_runner.rb +0 -1
  7. data/exe/steep +0 -1
  8. data/lib/steep.rb +33 -1
  9. data/lib/steep/annotation_parser.rb +4 -4
  10. data/lib/steep/ast/buffer.rb +11 -7
  11. data/lib/steep/ast/builtin.rb +8 -0
  12. data/lib/steep/ast/types/factory.rb +124 -89
  13. data/lib/steep/cli.rb +16 -1
  14. data/lib/steep/drivers/annotations.rb +1 -1
  15. data/lib/steep/drivers/check.rb +20 -4
  16. data/lib/steep/drivers/init.rb +5 -5
  17. data/lib/steep/drivers/langserver.rb +13 -287
  18. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  19. data/lib/steep/drivers/vendor.rb +2 -2
  20. data/lib/steep/drivers/watch.rb +97 -85
  21. data/lib/steep/drivers/worker.rb +51 -0
  22. data/lib/steep/project.rb +9 -5
  23. data/lib/steep/project/completion_provider.rb +298 -0
  24. data/lib/steep/project/dsl.rb +14 -0
  25. data/lib/steep/project/file.rb +54 -47
  26. data/lib/steep/project/hover_content.rb +17 -8
  27. data/lib/steep/project/options.rb +25 -3
  28. data/lib/steep/project/target.rb +40 -24
  29. data/lib/steep/server/base_worker.rb +56 -0
  30. data/lib/steep/server/code_worker.rb +151 -0
  31. data/lib/steep/server/interaction_worker.rb +281 -0
  32. data/lib/steep/server/master.rb +196 -0
  33. data/lib/steep/server/signature_worker.rb +148 -0
  34. data/lib/steep/server/utils.rb +36 -0
  35. data/lib/steep/server/worker_process.rb +62 -0
  36. data/lib/steep/signature/errors.rb +1 -1
  37. data/lib/steep/signature/validator.rb +13 -13
  38. data/lib/steep/source.rb +1 -1
  39. data/lib/steep/type_construction.rb +1004 -727
  40. data/lib/steep/type_inference/constant_env.rb +3 -11
  41. data/lib/steep/type_inference/context.rb +8 -3
  42. data/lib/steep/type_inference/context_array.rb +111 -0
  43. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  44. data/lib/steep/type_inference/logic.rb +130 -0
  45. data/lib/steep/type_inference/type_env.rb +5 -69
  46. data/lib/steep/typing.rb +91 -23
  47. data/lib/steep/version.rb +1 -1
  48. data/smoke/alias/Steepfile +1 -0
  49. data/smoke/alias/a.rb +1 -1
  50. data/smoke/and/Steepfile +1 -0
  51. data/smoke/array/Steepfile +1 -0
  52. data/smoke/array/b.rb +0 -2
  53. data/smoke/block/Steepfile +1 -0
  54. data/smoke/case/Steepfile +1 -0
  55. data/smoke/class/Steepfile +1 -0
  56. data/smoke/const/Steepfile +1 -0
  57. data/smoke/dstr/Steepfile +1 -0
  58. data/smoke/ensure/Steepfile +1 -0
  59. data/smoke/enumerator/Steepfile +1 -0
  60. data/smoke/extension/Steepfile +1 -0
  61. data/smoke/extension/c.rb +1 -0
  62. data/smoke/hash/Steepfile +1 -0
  63. data/smoke/hello/Steepfile +1 -0
  64. data/smoke/if/Steepfile +1 -0
  65. data/smoke/if/a.rb +1 -1
  66. data/smoke/implements/Steepfile +1 -0
  67. data/smoke/initialize/Steepfile +1 -0
  68. data/smoke/integer/Steepfile +1 -0
  69. data/smoke/interface/Steepfile +1 -0
  70. data/smoke/kwbegin/Steepfile +1 -0
  71. data/smoke/lambda/Steepfile +1 -0
  72. data/smoke/literal/Steepfile +1 -0
  73. data/smoke/map/Steepfile +1 -0
  74. data/smoke/method/Steepfile +1 -0
  75. data/smoke/module/Steepfile +1 -0
  76. data/smoke/regexp/Steepfile +1 -0
  77. data/smoke/regexp/b.rb +4 -4
  78. data/smoke/regression/Steepfile +1 -0
  79. data/smoke/rescue/Steepfile +1 -0
  80. data/smoke/rescue/a.rb +1 -1
  81. data/smoke/self/Steepfile +1 -0
  82. data/smoke/skip/Steepfile +1 -0
  83. data/smoke/stdout/Steepfile +1 -0
  84. data/smoke/super/Steepfile +1 -0
  85. data/smoke/type_case/Steepfile +1 -0
  86. data/smoke/yield/Steepfile +1 -0
  87. data/steep.gemspec +8 -8
  88. metadata +38 -138
  89. data/exe/rbs +0 -3
  90. data/exe/ruby-signature +0 -3
  91. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  92. data/vendor/ruby-signature/.gitignore +0 -12
  93. data/vendor/ruby-signature/.rubocop.yml +0 -15
  94. data/vendor/ruby-signature/BSDL +0 -22
  95. data/vendor/ruby-signature/COPYING +0 -56
  96. data/vendor/ruby-signature/Gemfile +0 -6
  97. data/vendor/ruby-signature/README.md +0 -93
  98. data/vendor/ruby-signature/Rakefile +0 -66
  99. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -156
  100. data/vendor/ruby-signature/bin/console +0 -14
  101. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  102. data/vendor/ruby-signature/bin/setup +0 -10
  103. data/vendor/ruby-signature/bin/sort +0 -88
  104. data/vendor/ruby-signature/bin/test_runner.rb +0 -17
  105. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  106. data/vendor/ruby-signature/docs/sigs.md +0 -148
  107. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  108. data/vendor/ruby-signature/docs/syntax.md +0 -528
  109. data/vendor/ruby-signature/exe/rbs +0 -3
  110. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  111. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -64
  112. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  113. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  114. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  115. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  116. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  117. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  118. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -534
  119. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  120. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  121. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  122. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  123. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  124. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  125. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  126. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  127. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  128. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  129. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  130. data/vendor/ruby-signature/lib/ruby/signature/parser.y +0 -1343
  131. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -441
  132. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  133. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  134. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  135. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  136. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  137. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  138. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +0 -58
  139. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -324
  140. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  141. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  142. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  143. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  144. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  145. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  146. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  147. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
  148. data/vendor/ruby-signature/ruby-signature.gemspec +0 -45
  149. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -3
  150. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -15
  151. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  152. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  153. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  154. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -35
  155. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  156. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  157. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  158. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  159. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  160. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  161. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -419
  162. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -606
  163. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  164. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  165. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  166. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  167. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -193
  168. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  169. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  170. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  171. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -476
  172. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  173. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  174. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -121
  175. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  176. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -710
  177. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  178. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -574
  179. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -135
  180. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -141
  181. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -66
  182. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -182
  183. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -248
  184. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  185. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  186. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  187. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -426
  188. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -354
  189. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -93
  190. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  191. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  192. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -10
  193. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -131
  194. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  195. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  196. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -770
  197. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -13
  198. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  199. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -230
  200. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1112
  201. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  202. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -739
  203. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -91
  204. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  205. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -159
  206. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  207. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -18
  208. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -44
  209. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -21
  210. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  211. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  212. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  213. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -77
  214. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,121 +0,0 @@
1
- # The [GC](GC) module provides an interface to Ruby’s
2
- # mark and sweep garbage collection mechanism.
3
- #
4
- # Some of the underlying methods are also available via the
5
- # [ObjectSpace](https://ruby-doc.org/core-2.6.3/ObjectSpace.html) module.
6
- #
7
- # You may obtain information about the operation of the
8
- # [GC](GC) through
9
- # [GC::Profiler](https://ruby-doc.org/core-2.6.3/GC/Profiler.html).
10
- module GC
11
- # The number of times [GC](GC.downloaded.ruby_doc) occurred.
12
- #
13
- # It returns the number of times [GC](GC.downloaded.ruby_doc) occurred
14
- # since the process started.
15
- def self.count: () -> Integer
16
-
17
- # Disables garbage collection, returning `true` if garbage collection was
18
- # already disabled.
19
- #
20
- # ```ruby
21
- # GC.disable #=> false
22
- # GC.disable #=> true
23
- # ```
24
- def self.disable: () -> bool
25
-
26
- # Enables garbage collection, returning `true` if garbage collection was
27
- # previously disabled.
28
- #
29
- # ```ruby
30
- # GC.disable #=> false
31
- # GC.enable #=> true
32
- # GC.enable #=> false
33
- # ```
34
- def self.enable: () -> bool
35
-
36
- def self.start: (?full_mark: bool full_mark, ?immediate_mark: bool immediate_mark, ?immediate_sweep: bool immediate_sweep) -> nil
37
-
38
- # Returns a [Hash](https://ruby-doc.org/core-2.6.3/Hash.html) containing
39
- # information about the [GC](GC.downloaded.ruby_doc).
40
- #
41
- # The hash includes information about internal statistics about
42
- # [GC](GC.downloaded.ruby_doc) such as:
43
- #
44
- # ```ruby
45
- # {
46
- # :count=>0,
47
- # :heap_allocated_pages=>24,
48
- # :heap_sorted_length=>24,
49
- # :heap_allocatable_pages=>0,
50
- # :heap_available_slots=>9783,
51
- # :heap_live_slots=>7713,
52
- # :heap_free_slots=>2070,
53
- # :heap_final_slots=>0,
54
- # :heap_marked_slots=>0,
55
- # :heap_eden_pages=>24,
56
- # :heap_tomb_pages=>0,
57
- # :total_allocated_pages=>24,
58
- # :total_freed_pages=>0,
59
- # :total_allocated_objects=>7796,
60
- # :total_freed_objects=>83,
61
- # :malloc_increase_bytes=>2389312,
62
- # :malloc_increase_bytes_limit=>16777216,
63
- # :minor_gc_count=>0,
64
- # :major_gc_count=>0,
65
- # :remembered_wb_unprotected_objects=>0,
66
- # :remembered_wb_unprotected_objects_limit=>0,
67
- # :old_objects=>0,
68
- # :old_objects_limit=>0,
69
- # :oldmalloc_increase_bytes=>2389760,
70
- # :oldmalloc_increase_bytes_limit=>16777216
71
- # }
72
- # ```
73
- #
74
- # The contents of the hash are implementation specific and may be changed
75
- # in the future.
76
- #
77
- # This method is only expected to work on C Ruby.
78
- def self.stat: (?::Hash[Symbol, Integer] arg0) -> ::Hash[Symbol, Integer]
79
- | (?Symbol arg0) -> Integer
80
-
81
- # Returns current status of [GC](GC.downloaded.ruby_doc) stress mode.
82
- def self.stress: () -> (Integer | TrueClass | FalseClass)
83
-
84
- def self.stress=: ((Integer | TrueClass | FalseClass) flag) -> (Integer | TrueClass | FalseClass)
85
-
86
- def self.compact: () -> ::Hash[:considered | :moved, Hash[Symbol | Integer, Integer]]
87
-
88
- def self.verify_compaction_references: () -> ::Hash[:considered | :moved, Hash[Symbol | Integer, Integer]]
89
-
90
- def self.verify_internal_consistency: () -> nil
91
-
92
- def self.verify_transient_heap_internal_consistency: () -> nil
93
-
94
- def self.latest_gc_info: () -> ::Hash[::Symbol, untyped]
95
- | [K] (?Hash[K, untyped] hash) -> ::Hash[::Symbol | K, untyped]
96
- | (Symbol key) -> untyped
97
-
98
- def garbage_collect: (?full_mark: bool full_mark, ?immediate_mark: bool immediate_mark, ?immediate_sweep: bool immediate_sweep) -> nil
99
- end
100
-
101
- GC::INTERNAL_CONSTANTS: Hash[Symbol, Integer]
102
-
103
- GC::OPTS: Array[String]
104
-
105
- module GC::Profiler
106
- def self.clear: () -> void
107
-
108
- def self.disable: () -> void
109
-
110
- def self.enable: () -> void
111
-
112
- def self.enabled?: () -> bool
113
-
114
- def self.raw_data: () -> ::Array[::Hash[Symbol, untyped]]
115
-
116
- def self.report: (?IO io) -> void
117
-
118
- def self.result: () -> String
119
-
120
- def self.total_time: () -> Float
121
- end
@@ -1,1029 +0,0 @@
1
- # A Hash is a dictionary-like collection of unique keys and their values. Also
2
- # called associative arrays, they are similar to Arrays, but where an Array uses
3
- # integers as its index, a Hash allows you to use any object type.
4
- #
5
- # Hashes enumerate their values in the order that the corresponding keys were
6
- # inserted.
7
- #
8
- # A Hash can be easily created by using its implicit form:
9
- #
10
- # grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
11
- #
12
- # Hashes allow an alternate syntax for keys that are symbols. Instead of
13
- #
14
- # options = { :font_size => 10, :font_family => "Arial" }
15
- #
16
- # You could write it as:
17
- #
18
- # options = { font_size: 10, font_family: "Arial" }
19
- #
20
- # Each named key is a symbol you can access in hash:
21
- #
22
- # options[:font_size] # => 10
23
- #
24
- # A Hash can also be created through its ::new method:
25
- #
26
- # grades = Hash.new
27
- # grades["Dorothy Doe"] = 9
28
- #
29
- # Hashes have a *default value* that is returned when accessing keys that do not
30
- # exist in the hash. If no default is set `nil` is used. You can set the default
31
- # value by sending it as an argument to Hash.new:
32
- #
33
- # grades = Hash.new(0)
34
- #
35
- # Or by using the #default= method:
36
- #
37
- # grades = {"Timmy Doe" => 8}
38
- # grades.default = 0
39
- #
40
- # Accessing a value in a Hash requires using its key:
41
- #
42
- # puts grades["Jane Doe"] # => 0
43
- #
44
- # ### Common Uses
45
- #
46
- # Hashes are an easy way to represent data structures, such as
47
- #
48
- # books = {}
49
- # books[:matz] = "The Ruby Programming Language"
50
- # books[:black] = "The Well-Grounded Rubyist"
51
- #
52
- # Hashes are also commonly used as a way to have named parameters in functions.
53
- # Note that no brackets are used below. If a hash is the last argument on a
54
- # method call, no braces are needed, thus creating a really clean interface:
55
- #
56
- # Person.create(name: "John Doe", age: 27)
57
- #
58
- # def self.create(params)
59
- # @name = params[:name]
60
- # @age = params[:age]
61
- # end
62
- #
63
- # ### Hash Keys
64
- #
65
- # Two objects refer to the same hash key when their `hash` value is identical
66
- # and the two objects are `eql?` to each other.
67
- #
68
- # A user-defined class may be used as a hash key if the `hash` and `eql?`
69
- # methods are overridden to provide meaningful behavior. By default, separate
70
- # instances refer to separate hash keys.
71
- #
72
- # A typical implementation of `hash` is based on the object's data while `eql?`
73
- # is usually aliased to the overridden `==` method:
74
- #
75
- # class Book
76
- # attr_reader :author, :title
77
- #
78
- # def initialize(author, title)
79
- # @author = author
80
- # @title = title
81
- # end
82
- #
83
- # def ==(other)
84
- # self.class === other and
85
- # other.author == @author and
86
- # other.title == @title
87
- # end
88
- #
89
- # alias eql? ==
90
- #
91
- # def hash
92
- # @author.hash ^ @title.hash # XOR
93
- # end
94
- # end
95
- #
96
- # book1 = Book.new 'matz', 'Ruby in a Nutshell'
97
- # book2 = Book.new 'matz', 'Ruby in a Nutshell'
98
- #
99
- # reviews = {}
100
- #
101
- # reviews[book1] = 'Great reference!'
102
- # reviews[book2] = 'Nice and compact!'
103
- #
104
- # reviews.length #=> 1
105
- #
106
- # See also Object#hash and Object#eql?
107
- #
108
- class Hash[unchecked out K, unchecked out V] < Object
109
- include Enumerable[[K, V], Hash[K, V]]
110
-
111
- # Creates a new hash populated with the given objects.
112
- #
113
- # Similar to the literal `{ *key* => *value*, ... }`. In the first form, keys
114
- # and values occur in pairs, so there must be an even number of arguments.
115
- #
116
- # The second and third form take a single argument which is either an array of
117
- # key-value pairs or an object convertible to a hash.
118
- #
119
- # Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
120
- # Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
121
- # Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
122
- #
123
- def self.[]: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
124
- | [U, V] (Array[[ U, V ]]) -> ::Hash[U, V]
125
- | (*untyped) -> ::Hash[untyped, untyped]
126
-
127
- # Try to convert *obj* into a hash, using to_hash method. Returns converted hash
128
- # or nil if *obj* cannot be converted for any reason.
129
- #
130
- # Hash.try_convert({1=>2}) # => {1=>2}
131
- # Hash.try_convert("1=>2") # => nil
132
- #
133
- def self.try_convert: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
134
- | (untyped) -> (::Hash[untyped, untyped] | nil)
135
-
136
- public
137
-
138
- # Returns `true` if *hash* is subset of *other*.
139
- #
140
- # h1 = {a:1, b:2}
141
- # h2 = {a:1, b:2, c:3}
142
- # h1 < h2 #=> true
143
- # h2 < h1 #=> false
144
- # h1 < h1 #=> false
145
- #
146
- def <: (::Hash[K, V]) -> bool
147
-
148
- # Returns `true` if *hash* is subset of *other* or equals to *other*.
149
- #
150
- # h1 = {a:1, b:2}
151
- # h2 = {a:1, b:2, c:3}
152
- # h1 <= h2 #=> true
153
- # h2 <= h1 #=> false
154
- # h1 <= h1 #=> true
155
- #
156
- def <=: (::Hash[K, V]) -> bool
157
-
158
- # Equality---Two hashes are equal if they each contain the same number of keys
159
- # and if each key-value pair is equal to (according to Object#==) the
160
- # corresponding elements in the other hash.
161
- #
162
- # h1 = { "a" => 1, "c" => 2 }
163
- # h2 = { 7 => 35, "c" => 2, "a" => 1 }
164
- # h3 = { "a" => 1, "c" => 2, 7 => 35 }
165
- # h4 = { "a" => 1, "d" => 2, "f" => 35 }
166
- # h1 == h2 #=> false
167
- # h2 == h3 #=> true
168
- # h3 == h4 #=> false
169
- #
170
- # The orders of each hashes are not compared.
171
- #
172
- # h1 = { "a" => 1, "c" => 2 }
173
- # h2 = { "c" => 2, "a" => 1 }
174
- # h1 == h2 #=> true
175
- #
176
- def ==: (untyped other) -> bool
177
-
178
- # Returns `true` if *other* is subset of *hash*.
179
- #
180
- # h1 = {a:1, b:2}
181
- # h2 = {a:1, b:2, c:3}
182
- # h1 > h2 #=> false
183
- # h2 > h1 #=> true
184
- # h1 > h1 #=> false
185
- #
186
- def >: (::Hash[K, V]) -> bool
187
-
188
- # Returns `true` if *other* is subset of *hash* or equals to *hash*.
189
- #
190
- # h1 = {a:1, b:2}
191
- # h2 = {a:1, b:2, c:3}
192
- # h1 >= h2 #=> false
193
- # h2 >= h1 #=> true
194
- # h1 >= h1 #=> true
195
- #
196
- def >=: (::Hash[K, V]) -> bool
197
-
198
- # Element Reference---Retrieves the *value* object corresponding to the *key*
199
- # object. If not found, returns the default value (see Hash::new for details).
200
- #
201
- # h = { "a" => 100, "b" => 200 }
202
- # h["a"] #=> 100
203
- # h["c"] #=> nil
204
- #
205
- def []: (K arg0) -> V
206
-
207
- # ## Element Assignment
208
- #
209
- # Associates the value given by `value` with the key given by `key`.
210
- #
211
- # h = { "a" => 100, "b" => 200 }
212
- # h["a"] = 9
213
- # h["c"] = 4
214
- # h #=> {"a"=>9, "b"=>200, "c"=>4}
215
- # h.store("d", 42) #=> 42
216
- # h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
217
- #
218
- # `key` should not have its value changed while it is in use as a key (an
219
- # `unfrozen String` passed as a key will be duplicated and frozen).
220
- #
221
- # a = "a"
222
- # b = "b".freeze
223
- # h = { a => 100, b => 200 }
224
- # h.key(100).equal? a #=> false
225
- # h.key(200).equal? b #=> true
226
- #
227
- def []=: (K arg0, V arg1) -> V
228
-
229
- # See also Enumerable#any?
230
- #
231
- def any?: () -> bool
232
- | (untyped pattern) -> bool
233
- | () { (K, V) -> bool } -> bool
234
-
235
- # Searches through the hash comparing *obj* with the key using `==`. Returns the
236
- # key-value pair (two elements array) or `nil` if no match is found. See
237
- # Array#assoc.
238
- #
239
- # h = {"colors" => ["red", "blue", "green"],
240
- # "letters" => ["a", "b", "c" ]}
241
- # h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
242
- # h.assoc("foo") #=> nil
243
- #
244
- def assoc: (K arg0) -> [K, V]?
245
-
246
- # Removes all key-value pairs from *hsh*.
247
- #
248
- # h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
249
- # h.clear #=> {}
250
- #
251
- def clear: () -> self
252
-
253
- # Returns a new hash with the nil values/key pairs removed
254
- #
255
- # h = { a: 1, b: false, c: nil }
256
- # h.compact #=> { a: 1, b: false }
257
- # h #=> { a: 1, b: false, c: nil }
258
- #
259
- def compact: () -> self
260
-
261
- # Removes all nil values from the hash. Returns nil if no changes were made,
262
- # otherwise returns the hash.
263
- #
264
- # h = { a: 1, b: false, c: nil }
265
- # h.compact! #=> { a: 1, b: false }
266
- #
267
- def compact!: () -> self?
268
-
269
- # Makes *hsh* compare its keys by their identity, i.e. it will consider exact
270
- # same objects as same keys.
271
- #
272
- # h1 = { "a" => 100, "b" => 200, :c => "c" }
273
- # h1["a"] #=> 100
274
- # h1.compare_by_identity
275
- # h1.compare_by_identity? #=> true
276
- # h1["a".dup] #=> nil # different objects.
277
- # h1[:c] #=> "c" # same symbols are all same.
278
- #
279
- def compare_by_identity: () -> self
280
-
281
- # Returns `true` if *hsh* will compare its keys by their identity. Also see
282
- # Hash#compare_by_identity.
283
- #
284
- def compare_by_identity?: () -> bool
285
-
286
- def deconstruct_keys: (Array[K] | nil) -> self
287
-
288
- # Returns the default value, the value that would be returned by *[hsh](key)* if
289
- # *key* did not exist in *hsh*. See also Hash::new and Hash#default=.
290
- #
291
- # h = Hash.new #=> {}
292
- # h.default #=> nil
293
- # h.default(2) #=> nil
294
- #
295
- # h = Hash.new("cat") #=> {}
296
- # h.default #=> "cat"
297
- # h.default(2) #=> "cat"
298
- #
299
- # h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
300
- # h.default #=> nil
301
- # h.default(2) #=> 20
302
- #
303
- def default: (?K arg0) -> V?
304
-
305
- # Sets the default value, the value returned for a key that does not exist in
306
- # the hash. It is not possible to set the default to a Proc that will be
307
- # executed on each key lookup.
308
- #
309
- # h = { "a" => 100, "b" => 200 }
310
- # h.default = "Go fish"
311
- # h["a"] #=> 100
312
- # h["z"] #=> "Go fish"
313
- # # This doesn't do what you might hope...
314
- # h.default = proc do |hash, key|
315
- # hash[key] = key + key
316
- # end
317
- # h[2] #=> #<Proc:0x401b3948@-:6>
318
- # h["cat"] #=> #<Proc:0x401b3948@-:6>
319
- #
320
- def default=: (V arg0) -> V
321
-
322
- # If Hash::new was invoked with a block, return that block, otherwise return
323
- # `nil`.
324
- #
325
- # h = Hash.new {|h,k| h[k] = k*k } #=> {}
326
- # p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
327
- # a = [] #=> []
328
- # p.call(a, 2)
329
- # a #=> [nil, nil, 4]
330
- #
331
- def default_proc: () -> (Proc | nil)
332
-
333
- # Sets the default proc to be executed on each failed key lookup.
334
- #
335
- # h.default_proc = proc do |hash, key|
336
- # hash[key] = key + key
337
- # end
338
- # h[2] #=> 4
339
- # h["cat"] #=> "catcat"
340
- #
341
- def default_proc=: (Proc | _ToProc | nil) -> (Proc | _ToProc | nil)
342
-
343
- # Deletes the key-value pair and returns the value from *hsh* whose key is equal
344
- # to *key*. If the key is not found, it returns *nil*. If the optional code
345
- # block is given and the key is not found, pass in the key and return the result
346
- # of *block*.
347
- #
348
- # h = { "a" => 100, "b" => 200 }
349
- # h.delete("a") #=> 100
350
- # h.delete("z") #=> nil
351
- # h.delete("z") { |el| "#{el} not found" } #=> "z not found"
352
- #
353
- def delete: (K arg0) -> V?
354
- | [U] (K arg0) { (K arg0) -> U } -> (U | V)
355
-
356
- # Deletes every key-value pair from *hsh* for which *block* evaluates to `true`.
357
- #
358
- # If no block is given, an enumerator is returned instead.
359
- #
360
- # h = { "a" => 100, "b" => 200, "c" => 300 }
361
- # h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
362
- #
363
- def delete_if: () { (K arg0, V arg1) -> bool } -> self
364
- | () -> ::Enumerator[[ K, V ], self]
365
-
366
- # Extracts the nested value specified by the sequence of *key* objects by
367
- # calling `dig` at each step, returning `nil` if any intermediate step is `nil`.
368
- #
369
- # h = { foo: {bar: {baz: 1}}}
370
- #
371
- # h.dig(:foo, :bar, :baz) #=> 1
372
- # h.dig(:foo, :zot, :xyz) #=> nil
373
- #
374
- # g = { foo: [10, 11, 12] }
375
- # g.dig(:foo, 1) #=> 11
376
- # g.dig(:foo, 1, 0) #=> TypeError: Integer does not have #dig method
377
- # g.dig(:foo, :bar) #=> TypeError: no implicit conversion of Symbol into Integer
378
- #
379
- def dig: (*untyped) -> untyped
380
-
381
- # Calls *block* once for each key in *hsh*, passing the key-value pair as
382
- # parameters.
383
- #
384
- # If no block is given, an enumerator is returned instead.
385
- #
386
- # h = { "a" => 100, "b" => 200 }
387
- # h.each {|key, value| puts "#{key} is #{value}" }
388
- #
389
- # *produces:*
390
- #
391
- # a is 100
392
- # b is 200
393
- #
394
- def each: () { ([ K, V ] arg0) -> untyped } -> self
395
- | () -> ::Enumerator[[ K, V ], self]
396
-
397
- # Calls *block* once for each key in *hsh*, passing the key as a parameter.
398
- #
399
- # If no block is given, an enumerator is returned instead.
400
- #
401
- # h = { "a" => 100, "b" => 200 }
402
- # h.each_key {|key| puts key }
403
- #
404
- # *produces:*
405
- #
406
- # a
407
- # b
408
- #
409
- def each_key: () { (K arg0) -> untyped } -> ::Hash[K, V]
410
- | () -> ::Enumerator[K, self]
411
-
412
- # Calls *block* once for each key in *hsh*, passing the key-value pair as
413
- # parameters.
414
- #
415
- # If no block is given, an enumerator is returned instead.
416
- #
417
- # h = { "a" => 100, "b" => 200 }
418
- # h.each {|key, value| puts "#{key} is #{value}" }
419
- #
420
- # *produces:*
421
- #
422
- # a is 100
423
- # b is 200
424
- #
425
- alias each_pair each
426
-
427
- # Calls *block* once for each key in *hsh*, passing the value as a parameter.
428
- #
429
- # If no block is given, an enumerator is returned instead.
430
- #
431
- # h = { "a" => 100, "b" => 200 }
432
- # h.each_value {|value| puts value }
433
- #
434
- # *produces:*
435
- #
436
- # 100
437
- # 200
438
- #
439
- def each_value: () { (V arg0) -> untyped } -> self
440
- | () -> ::Enumerator[V, self]
441
-
442
- # Returns `true` if *hsh* contains no key-value pairs.
443
- #
444
- # {}.empty? #=> true
445
- #
446
- def empty?: () -> bool
447
-
448
- # Returns `true` if *hash* and *other* are both hashes with the same content.
449
- # The orders of each hashes are not compared.
450
- #
451
- def eql?: (untyped) -> bool
452
-
453
- # Returns a value from the hash for the given key. If the key can't be found,
454
- # there are several options: With no other arguments, it will raise a KeyError
455
- # exception; if *default* is given, then that will be returned; if the optional
456
- # code block is specified, then that will be run and its result returned.
457
- #
458
- # h = { "a" => 100, "b" => 200 }
459
- # h.fetch("a") #=> 100
460
- # h.fetch("z", "go fish") #=> "go fish"
461
- # h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
462
- #
463
- # The following example shows that an exception is raised if the key is not
464
- # found and a default value is not supplied.
465
- #
466
- # h = { "a" => 100, "b" => 200 }
467
- # h.fetch("z")
468
- #
469
- # *produces:*
470
- #
471
- # prog.rb:2:in `fetch': key not found (KeyError)
472
- # from prog.rb:2
473
- #
474
- def fetch: (K arg0) -> V
475
- | [X] (K arg0, X arg1) -> (V | X)
476
- | [X] (K arg0) { (K arg0) -> X } -> (V | X)
477
-
478
- # Returns an array containing the values associated with the given keys but also
479
- # raises KeyError when one of keys can't be found. Also see Hash#values_at and
480
- # Hash#fetch.
481
- #
482
- # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
483
- #
484
- # h.fetch_values("cow", "cat") #=> ["bovine", "feline"]
485
- # h.fetch_values("cow", "bird") # raises KeyError
486
- # h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
487
- #
488
- def fetch_values: (*K) -> Array[V]
489
- | [X] (*K) { (K) -> X } -> (V | X)
490
-
491
- # Returns a new hash consisting of entries for which the block returns true.
492
- #
493
- # If no block is given, an enumerator is returned instead.
494
- #
495
- # h = { "a" => 100, "b" => 200, "c" => 300 }
496
- # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
497
- # h.select {|k,v| v < 200} #=> {"a" => 100}
498
- #
499
- # Hash#filter is an alias for Hash#select.
500
- #
501
- def filter: () { (K arg0, V arg1) -> bool } -> self
502
- | () -> ::Enumerator[[ K, V ], self]
503
-
504
- # Equivalent to Hash#keep_if, but returns `nil` if no changes were made.
505
- #
506
- # Hash#filter! is an alias for Hash#select!.
507
- #
508
- def filter!: () { (K arg0, V arg1) -> bool } -> self?
509
- | () -> ::Enumerator[[ K, V ], self?]
510
-
511
- # Returns a new array that is a one-dimensional flattening of this hash. That
512
- # is, for every key or value that is an array, extract its elements into the new
513
- # array. Unlike Array#flatten, this method does not flatten recursively by
514
- # default. The optional *level* argument determines the level of recursion to
515
- # flatten.
516
- #
517
- # a = {1=> "one", 2 => [2,"two"], 3 => "three"}
518
- # a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
519
- # a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
520
- #
521
- def flatten: () -> ::Array[K | V]
522
- | (1 level) -> ::Array[K | V]
523
- | (Integer level) -> Array[untyped]
524
-
525
- # Returns `true` if the given key is present in *hsh*.
526
- #
527
- # h = { "a" => 100, "b" => 200 }
528
- # h.has_key?("a") #=> true
529
- # h.has_key?("z") #=> false
530
- #
531
- # Note that #include? and #member? do not test member equality using `==` as do
532
- # other Enumerables.
533
- #
534
- # See also Enumerable#include?
535
- #
536
- def has_key?: (K arg0) -> bool
537
-
538
- # Returns `true` if the given value is present for some key in *hsh*.
539
- #
540
- # h = { "a" => 100, "b" => 200 }
541
- # h.value?(100) #=> true
542
- # h.value?(999) #=> false
543
- #
544
- def has_value?: (V arg0) -> bool
545
-
546
- # Compute a hash-code for this hash. Two hashes with the same content will have
547
- # the same hash code (and will compare using `eql?`).
548
- #
549
- # See also Object#hash.
550
- #
551
- def hash: () -> Integer
552
-
553
- # Returns `true` if the given key is present in *hsh*.
554
- #
555
- # h = { "a" => 100, "b" => 200 }
556
- # h.has_key?("a") #=> true
557
- # h.has_key?("z") #=> false
558
- #
559
- # Note that #include? and #member? do not test member equality using `==` as do
560
- # other Enumerables.
561
- #
562
- # See also Enumerable#include?
563
- #
564
- alias include? has_key?
565
-
566
- def index: (V) -> K?
567
-
568
- # Return the contents of this hash as a string.
569
- #
570
- # h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
571
- # h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
572
- #
573
- def inspect: () -> String
574
-
575
- # Returns a new hash created by using *hsh*'s values as keys, and the keys as
576
- # values. If a key with the same value already exists in the *hsh*, then the
577
- # last one defined will be used, the earlier value(s) will be discarded.
578
- #
579
- # h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
580
- # h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
581
- #
582
- # If there is no key with the same value, Hash#invert is involutive.
583
- #
584
- # h = { a: 1, b: 3, c: 4 }
585
- # h.invert.invert == h #=> true
586
- #
587
- # The condition, no key with the same value, can be tested by comparing the size
588
- # of inverted hash.
589
- #
590
- # # no key with the same value
591
- # h = { a: 1, b: 3, c: 4 }
592
- # h.size == h.invert.size #=> true
593
- #
594
- # # two (or more) keys has the same value
595
- # h = { a: 1, b: 3, c: 1 }
596
- # h.size == h.invert.size #=> false
597
- #
598
- def invert: () -> ::Hash[V, K]
599
-
600
- # Deletes every key-value pair from *hsh* for which *block* evaluates to
601
- # `false`.
602
- #
603
- # If no block is given, an enumerator is returned instead.
604
- #
605
- # See also Hash#select!.
606
- #
607
- def keep_if: () { (K, V) -> bool } -> self
608
- | () -> ::Enumerator[[ K, V ], self]
609
-
610
- # Returns the key of an occurrence of a given value. If the value is not found,
611
- # returns `nil`.
612
- #
613
- # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
614
- # h.key(200) #=> "b"
615
- # h.key(300) #=> "c"
616
- # h.key(999) #=> nil
617
- #
618
- alias key index
619
-
620
- # Returns `true` if the given key is present in *hsh*.
621
- #
622
- # h = { "a" => 100, "b" => 200 }
623
- # h.has_key?("a") #=> true
624
- # h.has_key?("z") #=> false
625
- #
626
- # Note that #include? and #member? do not test member equality using `==` as do
627
- # other Enumerables.
628
- #
629
- # See also Enumerable#include?
630
- #
631
- alias key? has_key?
632
-
633
- # Returns a new array populated with the keys from this hash. See also
634
- # Hash#values.
635
- #
636
- # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
637
- # h.keys #=> ["a", "b", "c", "d"]
638
- #
639
- def keys: () -> ::Array[K]
640
-
641
- # Returns the number of key-value pairs in the hash.
642
- #
643
- # h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
644
- # h.size #=> 4
645
- # h.delete("a") #=> 200
646
- # h.size #=> 3
647
- # h.length #=> 3
648
- #
649
- # Hash#length is an alias for Hash#size.
650
- #
651
- def length: () -> Integer
652
-
653
- # Returns `true` if the given key is present in *hsh*.
654
- #
655
- # h = { "a" => 100, "b" => 200 }
656
- # h.has_key?("a") #=> true
657
- # h.has_key?("z") #=> false
658
- #
659
- # Note that #include? and #member? do not test member equality using `==` as do
660
- # other Enumerables.
661
- #
662
- # See also Enumerable#include?
663
- #
664
- alias member? has_key?
665
-
666
- # Returns a new hash that combines the contents of the receiver and the contents
667
- # of the given hashes.
668
- #
669
- # If no block is given, entries with duplicate keys are overwritten with the
670
- # values from each `other_hash` successively, otherwise the value for each
671
- # duplicate key is determined by calling the block with the key, its value in
672
- # the receiver and its value in each `other_hash`.
673
- #
674
- # When called without any argument, returns a copy of the receiver.
675
- #
676
- # h1 = { "a" => 100, "b" => 200 }
677
- # h2 = { "b" => 246, "c" => 300 }
678
- # h3 = { "b" => 357, "d" => 400 }
679
- # h1.merge #=> {"a"=>100, "b"=>200}
680
- # h1.merge(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
681
- # h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
682
- # h1.merge(h2) {|key, oldval, newval| newval - oldval}
683
- # #=> {"a"=>100, "b"=>46, "c"=>300}
684
- # h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
685
- # #=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
686
- # h1 #=> {"a"=>100, "b"=>200}
687
- #
688
- def merge: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V]
689
- | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> (C) } -> ::Hash[A | K, B | V | C]
690
-
691
- # Adds the contents of the given hashes to the receiver.
692
- #
693
- # If no block is given, entries with duplicate keys are overwritten with the
694
- # values from each `other_hash` successively, otherwise the value for each
695
- # duplicate key is determined by calling the block with the key, its value in
696
- # the receiver and its value in each `other_hash`.
697
- #
698
- # h1 = { "a" => 100, "b" => 200 }
699
- # h1.merge! #=> {"a"=>100, "b"=>200}
700
- # h1 #=> {"a"=>100, "b"=>200}
701
- #
702
- # h1 = { "a" => 100, "b" => 200 }
703
- # h2 = { "b" => 246, "c" => 300 }
704
- # h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
705
- # h1 #=> {"a"=>100, "b"=>246, "c"=>300}
706
- #
707
- # h1 = { "a" => 100, "b" => 200 }
708
- # h2 = { "b" => 246, "c" => 300 }
709
- # h3 = { "b" => 357, "d" => 400 }
710
- # h1.merge!(h2, h3)
711
- # #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
712
- # h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
713
- #
714
- # h1 = { "a" => 100, "b" => 200 }
715
- # h2 = { "b" => 246, "c" => 300 }
716
- # h3 = { "b" => 357, "d" => 400 }
717
- # h1.merge!(h2, h3) {|key, v1, v2| v1 }
718
- # #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
719
- # h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
720
- #
721
- # Hash#update is an alias for Hash#merge!.
722
- #
723
- def merge!: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V]
724
- | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> (C) } -> ::Hash[A | K, B | V | C]
725
-
726
- # Searches through the hash comparing *obj* with the value using `==`. Returns
727
- # the first key-value pair (two-element array) that matches. See also
728
- # Array#rassoc.
729
- #
730
- # a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
731
- # a.rassoc("two") #=> [2, "two"]
732
- # a.rassoc("four") #=> nil
733
- #
734
- def rassoc: (V) -> [K, V]?
735
-
736
- # Rebuilds the hash based on the current hash values for each key. If values of
737
- # key objects have changed since they were inserted, this method will reindex
738
- # *hsh*. If Hash#rehash is called while an iterator is traversing the hash, a
739
- # RuntimeError will be raised in the iterator.
740
- #
741
- # a = [ "a", "b" ]
742
- # c = [ "c", "d" ]
743
- # h = { a => 100, c => 300 }
744
- # h[a] #=> 100
745
- # a[0] = "z"
746
- # h[a] #=> nil
747
- # h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}
748
- # h[a] #=> 100
749
- #
750
- def rehash: () -> self
751
-
752
- # Returns a new hash consisting of entries for which the block returns false.
753
- #
754
- # If no block is given, an enumerator is returned instead.
755
- #
756
- # h = { "a" => 100, "b" => 200, "c" => 300 }
757
- # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
758
- # h.reject {|k,v| v > 100} #=> {"a" => 100}
759
- #
760
- def reject: () -> ::Enumerator[[ K, V ], self]
761
- | () { (K arg0, V arg1) -> bool } -> self
762
-
763
- # Equivalent to Hash#delete_if, but returns `nil` if no changes were made.
764
- #
765
- def reject!: () -> ::Enumerator[[ K, V ], self?]
766
- | () { (K arg0, V arg1) -> bool } -> self?
767
-
768
- # Replaces the contents of *hsh* with the contents of *other_hash*.
769
- #
770
- # h = { "a" => 100, "b" => 200 }
771
- # h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
772
- #
773
- def replace: [A, B] (Hash[A, B] | _ToHash[A, B]) -> Hash[A, B]
774
-
775
- # Returns a new hash consisting of entries for which the block returns true.
776
- #
777
- # If no block is given, an enumerator is returned instead.
778
- #
779
- # h = { "a" => 100, "b" => 200, "c" => 300 }
780
- # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
781
- # h.select {|k,v| v < 200} #=> {"a" => 100}
782
- #
783
- # Hash#filter is an alias for Hash#select.
784
- #
785
- alias select filter
786
-
787
- # Equivalent to Hash#keep_if, but returns `nil` if no changes were made.
788
- #
789
- # Hash#filter! is an alias for Hash#select!.
790
- #
791
- alias select! filter!
792
-
793
- # Removes a key-value pair from *hsh* and returns it as the two-item array `[`
794
- # *key, value* `]`, or the hash's default value if the hash is empty.
795
- #
796
- # h = { 1 => "a", 2 => "b", 3 => "c" }
797
- # h.shift #=> [1, "a"]
798
- # h #=> {2=>"b", 3=>"c"}
799
- #
800
- def shift: () -> [ K, V ]?
801
-
802
- # Returns the number of key-value pairs in the hash.
803
- #
804
- # h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
805
- # h.size #=> 4
806
- # h.delete("a") #=> 200
807
- # h.size #=> 3
808
- # h.length #=> 3
809
- #
810
- # Hash#length is an alias for Hash#size.
811
- #
812
- alias size length
813
-
814
- # Returns a hash containing only the given keys and their values.
815
- #
816
- # h = { a: 100, b: 200, c: 300 }
817
- # h.slice(:a) #=> {:a=>100}
818
- # h.slice(:b, :c, :d) #=> {:b=>200, :c=>300}
819
- #
820
- def slice: (*K) -> self
821
-
822
- # ## Element Assignment
823
- #
824
- # Associates the value given by `value` with the key given by `key`.
825
- #
826
- # h = { "a" => 100, "b" => 200 }
827
- # h["a"] = 9
828
- # h["c"] = 4
829
- # h #=> {"a"=>9, "b"=>200, "c"=>4}
830
- # h.store("d", 42) #=> 42
831
- # h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
832
- #
833
- # `key` should not have its value changed while it is in use as a key (an
834
- # `unfrozen String` passed as a key will be duplicated and frozen).
835
- #
836
- # a = "a"
837
- # b = "b".freeze
838
- # h = { a => 100, b => 200 }
839
- # h.key(100).equal? a #=> false
840
- # h.key(200).equal? b #=> true
841
- #
842
- alias store []=
843
-
844
- # Converts *hsh* to a nested array of `[` *key, value* `]` arrays.
845
- #
846
- # h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
847
- # h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
848
- #
849
- def to_a: () -> ::Array[[ K, V ]]
850
-
851
- # Returns `self`. If called on a subclass of Hash, converts the receiver to a
852
- # Hash object.
853
- #
854
- # If a block is given, the results of the block on each pair of the receiver
855
- # will be used as pairs.
856
- #
857
- def to_h: () -> Hash[K, V]
858
- | [A, B] () { (K, V) -> [ A, B ] } -> Hash[A, B]
859
-
860
- # Returns `self`.
861
- #
862
- def to_hash: () -> ::Hash[K, V]
863
-
864
- # Returns a Proc which maps keys to values.
865
- #
866
- # h = {a:1, b:2}
867
- # hp = h.to_proc
868
- # hp.call(:a) #=> 1
869
- # hp.call(:b) #=> 2
870
- # hp.call(:c) #=> nil
871
- # [:a, :b, :c].map(&h) #=> [1, 2, nil]
872
- #
873
- def to_proc: () -> ^(K) -> V?
874
-
875
- alias to_s inspect
876
-
877
- # Returns a new hash with the results of running the block once for every key.
878
- # This method does not change the values.
879
- #
880
- # h = { a: 1, b: 2, c: 3 }
881
- # h.transform_keys {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
882
- # h.transform_keys(&:to_s) #=> { "a" => 1, "b" => 2, "c" => 3 }
883
- # h.transform_keys.with_index {|k, i| "#{k}.#{i}" }
884
- # #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
885
- #
886
- # If no block is given, an enumerator is returned instead.
887
- #
888
- def transform_keys: () -> Enumerator[K, Hash[untyped, V]]
889
- | [A] () { (K) -> A } -> Hash[A, V]
890
-
891
- # Invokes the given block once for each key in *hsh*, replacing it with the new
892
- # key returned by the block, and then returns *hsh*. This method does not change
893
- # the values.
894
- #
895
- # h = { a: 1, b: 2, c: 3 }
896
- # h.transform_keys! {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
897
- # h.transform_keys!(&:to_sym) #=> { a: 1, b: 2, c: 3 }
898
- # h.transform_keys!.with_index {|k, i| "#{k}.#{i}" }
899
- # #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
900
- #
901
- # If no block is given, an enumerator is returned instead.
902
- #
903
- def transform_keys!: () -> Enumerator[K, Hash[untyped, V]]
904
- | () { (K) -> K } -> Hash[K, V]
905
-
906
- # Returns a new hash with the results of running the block once for every value.
907
- # This method does not change the keys.
908
- #
909
- # h = { a: 1, b: 2, c: 3 }
910
- # h.transform_values {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
911
- # h.transform_values(&:to_s) #=> { a: "1", b: "2", c: "3" }
912
- # h.transform_values.with_index {|v, i| "#{v}.#{i}" }
913
- # #=> { a: "1.0", b: "2.1", c: "3.2" }
914
- #
915
- # If no block is given, an enumerator is returned instead.
916
- #
917
- def transform_values: () -> Enumerator[K, Hash[K, untyped]]
918
- | [A] () { (V) -> A } -> Hash[K, A]
919
-
920
- # Invokes the given block once for each value in *hsh*, replacing it with the
921
- # new value returned by the block, and then returns *hsh*. This method does not
922
- # change the keys.
923
- #
924
- # h = { a: 1, b: 2, c: 3 }
925
- # h.transform_values! {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
926
- # h.transform_values!(&:to_s) #=> { a: "2", b: "5", c: "10" }
927
- # h.transform_values!.with_index {|v, i| "#{v}.#{i}" }
928
- # #=> { a: "2.0", b: "5.1", c: "10.2" }
929
- #
930
- # If no block is given, an enumerator is returned instead.
931
- #
932
- def transform_values!: () -> Enumerator[K, Hash[K, untyped]]
933
- | () { (V) -> V } -> Hash[K, V]
934
-
935
- # Adds the contents of the given hashes to the receiver.
936
- #
937
- # If no block is given, entries with duplicate keys are overwritten with the
938
- # values from each `other_hash` successively, otherwise the value for each
939
- # duplicate key is determined by calling the block with the key, its value in
940
- # the receiver and its value in each `other_hash`.
941
- #
942
- # h1 = { "a" => 100, "b" => 200 }
943
- # h1.merge! #=> {"a"=>100, "b"=>200}
944
- # h1 #=> {"a"=>100, "b"=>200}
945
- #
946
- # h1 = { "a" => 100, "b" => 200 }
947
- # h2 = { "b" => 246, "c" => 300 }
948
- # h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
949
- # h1 #=> {"a"=>100, "b"=>246, "c"=>300}
950
- #
951
- # h1 = { "a" => 100, "b" => 200 }
952
- # h2 = { "b" => 246, "c" => 300 }
953
- # h3 = { "b" => 357, "d" => 400 }
954
- # h1.merge!(h2, h3)
955
- # #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
956
- # h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
957
- #
958
- # h1 = { "a" => 100, "b" => 200 }
959
- # h2 = { "b" => 246, "c" => 300 }
960
- # h3 = { "b" => 357, "d" => 400 }
961
- # h1.merge!(h2, h3) {|key, v1, v2| v1 }
962
- # #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
963
- # h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
964
- #
965
- # Hash#update is an alias for Hash#merge!.
966
- #
967
- alias update merge!
968
-
969
- # Returns `true` if the given value is present for some key in *hsh*.
970
- #
971
- # h = { "a" => 100, "b" => 200 }
972
- # h.value?(100) #=> true
973
- # h.value?(999) #=> false
974
- #
975
- alias value? has_value?
976
-
977
- # Returns a new array populated with the values from *hsh*. See also Hash#keys.
978
- #
979
- # h = { "a" => 100, "b" => 200, "c" => 300 }
980
- # h.values #=> [100, 200, 300]
981
- #
982
- def values: () -> ::Array[V]
983
-
984
- # Return an array containing the values associated with the given keys. Also see
985
- # Hash.select.
986
- #
987
- # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
988
- # h.values_at("cow", "cat") #=> ["bovine", "feline"]
989
- #
990
- def values_at: (*K arg0) -> ::Array[V?]
991
-
992
- private
993
-
994
- # Returns a new, empty hash. If this hash is subsequently accessed by a key that
995
- # doesn't correspond to a hash entry, the value returned depends on the style of
996
- # `new` used to create the hash. In the first form, the access returns `nil`. If
997
- # *obj* is specified, this single object will be used for all *default values*.
998
- # If a block is specified, it will be called with the hash object and the key,
999
- # and should return the default value. It is the block's responsibility to store
1000
- # the value in the hash if required.
1001
- #
1002
- # h = Hash.new("Go Fish")
1003
- # h["a"] = 100
1004
- # h["b"] = 200
1005
- # h["a"] #=> 100
1006
- # h["c"] #=> "Go Fish"
1007
- # # The following alters the single default object
1008
- # h["c"].upcase! #=> "GO FISH"
1009
- # h["d"] #=> "GO FISH"
1010
- # h.keys #=> ["a", "b"]
1011
- #
1012
- # # While this creates a new default object each time
1013
- # h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
1014
- # h["c"] #=> "Go Fish: c"
1015
- # h["c"].upcase! #=> "GO FISH: C"
1016
- # h["d"] #=> "Go Fish: d"
1017
- # h.keys #=> ["c", "d"]
1018
- #
1019
- incompatible def initialize: () -> void
1020
- | (untyped default) -> void
1021
- | [A, B] () { (Hash[A, B] hash, A key) -> B } -> void
1022
-
1023
- # Replaces the contents of *hsh* with the contents of *other_hash*.
1024
- #
1025
- # h = { "a" => 100, "b" => 200 }
1026
- # h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1027
- #
1028
- def initialize_copy: (self object) -> self
1029
- end