-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathhash.rbs
More file actions
2182 lines (2100 loc) · 71.1 KB
/
Copy pathhash.rbs
File metadata and controls
2182 lines (2100 loc) · 71.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=hash.c -->
# A Hash object maps each of its unique keys to a specific value.
#
# A hash has certain similarities to an Array, but:
#
# * An array index is always an integer.
# * A hash key can be (almost) any object.
#
# ### Hash Data Syntax
#
# The original syntax for a hash entry uses the "hash rocket," <code>=></code>:
#
# h = {:foo => 0, :bar => 1, :baz => 2}
# h # => {foo: 0, bar: 1, baz: 2}
#
# Alternatively, but only for a key that's a symbol, you can use a newer
# JSON-style syntax, where each bareword becomes a symbol:
#
# h = {foo: 0, bar: 1, baz: 2}
# h # => {foo: 0, bar: 1, baz: 2}
#
# You can also use a string in place of a bareword:
#
# h = {'foo': 0, 'bar': 1, 'baz': 2}
# h # => {foo: 0, bar: 1, baz: 2}
#
# And you can mix the styles:
#
# h = {foo: 0, :bar => 1, 'baz': 2}
# h # => {foo: 0, bar: 1, baz: 2}
#
# But it's an error to try the JSON-style syntax for a key that's not a bareword
# or a string:
#
# # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
# h = {0: 'zero'}
#
# The value can be omitted, meaning that value will be fetched from the context
# by the name of the key:
#
# x = 0
# y = 100
# h = {x:, y:}
# h # => {x: 0, y: 100}
#
# ### Common Uses
#
# You can use a hash to give names to objects:
#
# person = {name: 'Matz', language: 'Ruby'}
# person # => {name: "Matz", language: "Ruby"}
#
# You can use a hash to give names to method arguments:
#
# def some_method(hash)
# p hash
# end
# some_method({foo: 0, bar: 1, baz: 2}) # => {foo: 0, bar: 1, baz: 2}
#
# Note: when the last argument in a method call is a hash, the curly braces may
# be omitted:
#
# some_method(foo: 0, bar: 1, baz: 2) # => {foo: 0, bar: 1, baz: 2}
#
# You can use a hash to initialize an object:
#
# class Dev
# attr_accessor :name, :language
# def initialize(hash)
# self.name = hash[:name]
# self.language = hash[:language]
# end
# end
# matz = Dev.new(name: 'Matz', language: 'Ruby')
# matz # => #<Dev: @name="Matz", @language="Ruby">
#
# ### Creating a Hash
#
# You can create a Hash object explicitly with:
#
# * A [hash literal](rdoc-ref:syntax/literals.rdoc@Hash+Literals).
#
# You can convert certain objects to hashes with:
#
# * Method Kernel#Hash.
#
# You can create a hash by calling method Hash.new:
#
# # Create an empty hash.
# h = Hash.new
# h # => {}
# h.class # => Hash
#
# You can create a hash by calling method Hash.[]:
#
# # Create an empty hash.
# h = Hash[]
# h # => {}
# # Create a hash with initial entries.
# h = Hash[foo: 0, bar: 1, baz: 2]
# h # => {foo: 0, bar: 1, baz: 2}
#
# You can create a hash by using its literal form (curly braces):
#
# # Create an empty hash.
# h = {}
# h # => {}
# # Create a +Hash+ with initial entries.
# h = {foo: 0, bar: 1, baz: 2}
# h # => {foo: 0, bar: 1, baz: 2}
#
# ### Hash Value Basics
#
# The simplest way to retrieve a hash value (instance method #[]):
#
# h = {foo: 0, bar: 1, baz: 2}
# h[:foo] # => 0
#
# The simplest way to create or update a hash value (instance method #[]=):
#
# h = {foo: 0, bar: 1, baz: 2}
# h[:bat] = 3 # => 3
# h # => {foo: 0, bar: 1, baz: 2, bat: 3}
# h[:foo] = 4 # => 4
# h # => {foo: 4, bar: 1, baz: 2, bat: 3}
#
# The simplest way to delete a hash entry (instance method #delete):
#
# h = {foo: 0, bar: 1, baz: 2}
# h.delete(:bar) # => 1
# h # => {foo: 0, baz: 2}
#
# ### Entry Order
#
# A Hash object presents its entries in the order of their creation. This is
# seen in:
#
# * Iterative methods such as `each`, `each_key`, `each_pair`, `each_value`.
# * Other order-sensitive methods such as `shift`, `keys`, `values`.
# * The string returned by method `inspect`.
#
# A new hash has its initial ordering per the given entries:
#
# h = Hash[foo: 0, bar: 1]
# h # => {foo: 0, bar: 1}
#
# New entries are added at the end:
#
# h[:baz] = 2
# h # => {foo: 0, bar: 1, baz: 2}
#
# Updating a value does not affect the order:
#
# h[:baz] = 3
# h # => {foo: 0, bar: 1, baz: 3}
#
# But re-creating a deleted entry can affect the order:
#
# h.delete(:foo)
# h[:foo] = 5
# h # => {bar: 1, baz: 3, foo: 5}
#
# ### `Hash` Keys
#
# #### `Hash` Key Equivalence
#
# Two objects are treated as the same hash key when their `hash` value is
# identical and the two objects are <code>eql?</code> to each other.
#
# #### Modifying an Active `Hash` Key
#
# Modifying a `Hash` key while it is in use damages the hash's index.
#
# This `Hash` has keys that are Arrays:
#
# a0 = [ :foo, :bar ]
# a1 = [ :baz, :bat ]
# h = {a0 => 0, a1 => 1}
# h.include?(a0) # => true
# h[a0] # => 0
# a0.hash # => 110002110
#
# Modifying array element <code>a0[0]</code> changes its hash value:
#
# a0[0] = :bam
# a0.hash # => 1069447059
#
# And damages the `Hash` index:
#
# h.include?(a0) # => false
# h[a0] # => nil
#
# You can repair the hash index using method `rehash`:
#
# h.rehash # => {[:bam, :bar]=>0, [:baz, :bat]=>1}
# h.include?(a0) # => true
# h[a0] # => 0
#
# A String key is always safe. That's because an unfrozen String passed as a key
# will be replaced by a duplicated and frozen String:
#
# s = 'foo'
# s.frozen? # => false
# h = {s => 0}
# first_key = h.keys.first
# first_key.frozen? # => true
#
# #### User-Defined `Hash` Keys
#
# To be usable as a `Hash` key, objects must implement the methods `hash` and
# <code>eql?</code>. Note: this requirement does not apply if the `Hash` uses
# #compare_by_identity since comparison will then rely on the keys' object id
# instead of `hash` and <code>eql?</code>.
#
# Object defines basic implementation for `hash` and <code>eq?</code> that makes
# each object a distinct key. Typically, user-defined classes will want to
# override these methods to provide meaningful behavior, or for example inherit
# Struct that has useful definitions for these.
#
# A typical implementation of `hash` is based on the object's data while
# <code>eql?</code> is usually aliased to the overridden <code>==</code> method:
#
# class Book
# attr_reader :author, :title
#
# def initialize(author, title)
# @author = author
# @title = title
# end
#
# def ==(other)
# self.class === other &&
# other.author == @author &&
# other.title == @title
# end
#
# alias eql? ==
#
# def hash
# [self.class, @author, @title].hash
# end
# end
#
# book1 = Book.new 'matz', 'Ruby in a Nutshell'
# book2 = Book.new 'matz', 'Ruby in a Nutshell'
#
# reviews = {}
#
# reviews[book1] = 'Great reference!'
# reviews[book2] = 'Nice and compact!'
#
# reviews.length #=> 1
#
# ### Key Not Found?
#
# When a method tries to retrieve and return the value for a key and that key
# *is found*, the returned value is the value associated with the key.
#
# But what if the key *is not found*? In that case, certain methods will return
# a default value while other will raise a KeyError.
#
# #### Nil Return Value
#
# If you want `nil` returned for a not-found key, you can call:
#
# * #[](key) (usually written as <code>#[key]</code>.
# * #assoc(key).
# * #dig(key, *identifiers).
# * #values_at(*keys).
#
# You can override these behaviors for #[], #dig, and #values_at (but not
# #assoc); see [Hash Default](rdoc-ref:Hash@Hash+Default).
#
# #### KeyError
#
# If you want KeyError raised for a not-found key, you can call:
#
# * #fetch(key).
# * #fetch_values(*keys).
#
# #### Hash Default
#
# For certain methods (#[], #dig, and #values_at), the return value for a
# not-found key is determined by two hash properties:
#
# * *default value*: returned by method #default.
# * *default proc*: returned by method #default_proc.
#
# In the simple case, both values are `nil`, and the methods return `nil` for a
# not-found key; see [Nil Return Value](rdoc-ref:Hash@Nil+Return+Value) above.
#
# Note that this entire section ("Hash Default"):
#
# * Applies *only* to methods #[], #dig, and #values_at.
# * Does *not* apply to methods #assoc, #fetch, or #fetch_values, which are
# not affected by the default value or default proc.
#
# ##### Any-Key Default
#
# You can define an any-key default for a hash; that is, a value that will be
# returned for *any* not-found key:
#
# * The value of #default_proc *must be* `nil`.
# * The value of #default (which may be any object, including `nil`) will be
# returned for a not-found key.
#
# You can set the default value when the hash is created with Hash.new and
# option `default_value`, or later with method #default=.
#
# Note: although the value of #default may be any object, it may not be a good
# idea to use a mutable object.
#
# ##### Per-Key Defaults
#
# You can define a per-key default for a hash; that is, a Proc that will return
# a value based on the key itself.
#
# You can set the default proc when the hash is created with Hash.new and a
# block, or later with method #default_proc=.
#
# Note that the proc can modify `self`, but modifying `self` in this way is not
# thread-safe; multiple threads can concurrently call into the default proc for
# the same key.
#
# #### Method Default
#
# For two methods, you can specify a default value for a not-found key that has
# effect only for a single method call (and not for any subsequent calls):
#
# * For method #fetch, you can specify an any-key default:
# * For either method #fetch or method #fetch_values, you can specify a
# per-key default via a block.
#
# ### What's Here
#
# First, what's elsewhere. Class `Hash`:
#
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
# provides dozens of additional methods.
#
# Here, class `Hash` provides methods that are useful for:
#
# * [Creating a Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash)
# * [Setting Hash State](rdoc-ref:Hash@Methods+for+Setting+Hash+State)
# * [Querying](rdoc-ref:Hash@Methods+for+Querying)
# * [Comparing](rdoc-ref:Hash@Methods+for+Comparing)
# * [Fetching](rdoc-ref:Hash@Methods+for+Fetching)
# * [Assigning](rdoc-ref:Hash@Methods+for+Assigning)
# * [Deleting](rdoc-ref:Hash@Methods+for+Deleting)
# * [Iterating](rdoc-ref:Hash@Methods+for+Iterating)
# * [Converting](rdoc-ref:Hash@Methods+for+Converting)
# * [Transforming Keys and
# Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values)
#
# Class `Hash` also includes methods from module Enumerable.
#
# #### Methods for Creating a `Hash`
#
# * ::[]: Returns a new hash populated with given objects.
# * ::new: Returns a new empty hash.
# * ::try_convert: Returns a new hash created from a given object.
#
# #### Methods for Setting `Hash` State
#
# * #compare_by_identity: Sets `self` to consider only identity in comparing
# keys.
# * #default=: Sets the default to a given value.
# * #default_proc=: Sets the default proc to a given proc.
# * #rehash: Rebuilds the hash table by recomputing the hash index for each
# key.
#
# #### Methods for Querying
#
# * #any?: Returns whether any element satisfies a given criterion.
# * #compare_by_identity?: Returns whether the hash considers only identity
# when comparing keys.
# * #default: Returns the default value, or the default value for a given key.
# * #default_proc: Returns the default proc.
# * #empty?: Returns whether there are no entries.
# * #eql?: Returns whether a given object is equal to `self`.
# * #hash: Returns the integer hash code.
# * #has_value? (aliased as #value?): Returns whether a given object is a
# value in `self`.
# * #include? (aliased as #has_key?, #member?, #key?): Returns whether a given
# object is a key in `self`.
# * #size (aliased as #length): Returns the count of entries.
#
# #### Methods for Comparing
#
# * #<: Returns whether `self` is a proper subset of a given object.
# * #<=: Returns whether `self` is a subset of a given object.
# * #==: Returns whether a given object is equal to `self`.
# * #>: Returns whether `self` is a proper superset of a given object
# * #>=: Returns whether `self` is a superset of a given object.
#
# #### Methods for Fetching
#
# * #[]: Returns the value associated with a given key.
# * #assoc: Returns a 2-element array containing a given key and its value.
# * #dig: Returns the object in nested objects that is specified by a given
# key and additional arguments.
# * #fetch: Returns the value for a given key.
# * #fetch_values: Returns array containing the values associated with given
# keys.
# * #key: Returns the key for the first-found entry with a given value.
# * #keys: Returns an array containing all keys in `self`.
# * #rassoc: Returns a 2-element array consisting of the key and value of the
# first-found entry having a given value.
# * #values: Returns an array containing all values in `self`.
# * #values_at: Returns an array containing values for given keys.
#
# #### Methods for Assigning
#
# * #[]= (aliased as #store): Associates a given key with a given value.
# * #merge: Returns the hash formed by merging each given hash into a copy of
# `self`.
# * #update (aliased as #merge!): Merges each given hash into `self`.
# * #replace (aliased as #initialize_copy): Replaces the entire contents of
# `self` with the contents of a given hash.
#
# #### Methods for Deleting
#
# These methods remove entries from `self`:
#
# * #clear: Removes all entries from `self`.
# * #compact!: Removes all `nil`-valued entries from `self`.
# * #delete: Removes the entry for a given key.
# * #delete_if: Removes entries selected by a given block.
# * #select! (aliased as #filter!): Keep only those entries selected by a
# given block.
# * #keep_if: Keep only those entries selected by a given block.
# * #reject!: Removes entries selected by a given block.
# * #shift: Removes and returns the first entry.
#
# These methods return a copy of `self` with some entries removed:
#
# * #compact: Returns a copy of `self` with all `nil`-valued entries removed.
# * #except: Returns a copy of `self` with entries removed for specified keys.
# * #select (aliased as #filter): Returns a copy of `self` with only those
# entries selected by a given block.
# * #reject: Returns a copy of `self` with entries removed as specified by a
# given block.
# * #slice: Returns a hash containing the entries for given keys.
#
# #### Methods for Iterating
# * #each_pair (aliased as #each): Calls a given block with each key-value
# pair.
# * #each_key: Calls a given block with each key.
# * #each_value: Calls a given block with each value.
#
# #### Methods for Converting
#
# * #flatten: Returns an array that is a 1-dimensional flattening of `self`.
# * #inspect (aliased as #to_s): Returns a new String containing the hash
# entries.
# * #to_a: Returns a new array of 2-element arrays; each nested array contains
# a key-value pair from `self`.
# * #to_h: Returns `self` if a `Hash`; if a subclass of `Hash`, returns a
# `Hash` containing the entries from `self`.
# * #to_hash: Returns `self`.
# * #to_proc: Returns a proc that maps a given key to its value.
#
# #### Methods for Transforming Keys and Values
#
# * #invert: Returns a hash with the each key-value pair inverted.
# * #transform_keys: Returns a copy of `self` with modified keys.
# * #transform_keys!: Modifies keys in `self`
# * #transform_values: Returns a copy of `self` with modified values.
# * #transform_values!: Modifies values in `self`.
#
class Hash[unchecked out K, unchecked out V]
include Enumerable[[ K, V ]]
# Interface that indicates a type can be used as a key to `Hash` lookup methods.
#
interface _Key
def hash: () -> Integer
def eql?: (untyped rhs) -> boolish
end
# Interface that indicates a type convertible to `[ K, V ]` via its `#to_ary` method.
#
interface _Pair[K, V]
def to_ary: () -> [ K, V ]
end
%a{deprecated: Use RBS::Ops::_Equals}
interface _Equals
def ==: (untyped other) -> boolish
end
# <!--
# rdoc-file=hash.c
# - Hash[] -> new_empty_hash
# - Hash[other_hash] -> new_hash
# - Hash[ [*2_element_arrays] ] -> new_hash
# - Hash[*objects] -> new_hash
# -->
# Returns a new Hash object populated with the given objects, if any. See
# Hash::new.
#
# With no argument given, returns a new empty hash.
#
# With a single argument `other_hash` given that is a hash, returns a new hash
# initialized with the entries from that hash (but not with its `default` or
# `default_proc`):
#
# h = {foo: 0, bar: 1, baz: 2}
# Hash[h] # => {foo: 0, bar: 1, baz: 2}
#
# With a single argument `2_element_arrays` given that is an array of 2-element
# arrays, returns a new hash wherein each given 2-element array forms a
# key-value entry:
#
# Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {foo: 0, bar: 1}
#
# With an even number of arguments `objects` given, returns a new hash wherein
# each successive pair of arguments is a key-value entry:
#
# Hash[:foo, 0, :bar, 1] # => {foo: 0, bar: 1}
#
# Raises ArgumentError if the argument list does not conform to any of the
# above.
#
# See also [Methods for Creating a
# Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash).
#
def self.[]: [K, V] (hash[K, V] hash) -> instance
| [K, V] (array[ _Pair[K, V] ] two_element_arrays) -> instance
| [T] (*T objects) -> Hash[T, T]
# <!--
# rdoc-file=hash.c
# - Hash.try_convert(object) -> object, new_hash, or nil
# -->
# If `object` is a hash, returns `object`.
#
# Otherwise if `object` responds to <code>:to_hash</code>, calls
# <code>object.to_hash</code>; returns the result if it is a hash, or raises
# TypeError if not.
#
# Otherwise if `object` does not respond to <code>:to_hash</code>, returns
# `nil`.
#
def self.try_convert: [K, V] (hash[K, V] hash) -> Hash[K, V]
| (untyped) -> Hash[untyped, untyped]?
# <!--
# rdoc-file=hash.c
# - self < other -> true or false
# -->
# Returns whether the entries of `self` are a proper subset of the entries of
# `other`:
#
# h = {foo: 0, bar: 1}
# h < {foo: 0, bar: 1, baz: 2} # => true # Proper subset.
# h < {baz: 2, bar: 1, foo: 0} # => true # Order may differ.
# h < h # => false # Not a proper subset.
# h < {bar: 1, foo: 0} # => false # Not a proper subset.
# h < {foo: 0, bar: 1, baz: 2} # => false # Different key.
# h < {foo: 0, bar: 1, baz: 2} # => false # Different value.
#
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
#
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
# hash.
#
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def <: (hash[untyped, untyped] other_hash) -> bool
# <!--
# rdoc-file=hash.c
# - self <= other -> true or false
# -->
# Returns whether the entries of `self` are a subset of the entries of `other`:
#
# h0 = {foo: 0, bar: 1}
# h1 = {foo: 0, bar: 1, baz: 2}
# h0 <= h0 # => true
# h0 <= h1 # => true
# h1 <= h0 # => false
#
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
#
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
# hash.
#
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def <=: (hash[untyped, untyped] other_hash) -> bool
# <!--
# rdoc-file=hash.c
# - self == object -> true or false
# -->
# Returns whether `self` and `object` are equal.
#
# Returns `true` if all of the following are true:
#
# * `object` is a `Hash` object (or can be converted to one).
# * `self` and `object` have the same keys (regardless of order).
# * For each key `key`, <code>self[key] == object[key]</code>.
#
# Otherwise, returns `false`.
#
# Examples:
#
# h = {foo: 0, bar: 1}
# h == {foo: 0, bar: 1} # => true # Equal entries (same order)
# h == {bar: 1, foo: 0} # => true # Equal entries (different order).
# h == 1 # => false # Object not a hash.
# h == {} # => false # Different number of entries.
# h == {foo: 0, bar: 1} # => false # Different key.
# h == {foo: 0, bar: 1} # => false # Different value.
#
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def ==: (untyped other) -> bool
# <!--
# rdoc-file=hash.c
# - self > other_hash -> true or false
# -->
# Returns `true` if the entries of `self` are a proper superset of the entries
# of `other_hash`, `false` otherwise:
#
# h = {foo: 0, bar: 1, baz: 2}
# h > {foo: 0, bar: 1} # => true # Proper superset.
# h > {bar: 1, foo: 0} # => true # Order may differ.
# h > h # => false # Not a proper superset.
# h > {baz: 2, bar: 1, foo: 0} # => false # Not a proper superset.
# h > {foo: 0, bar: 1} # => false # Different key.
# h > {foo: 0, bar: 1} # => false # Different value.
#
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
#
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
# hash.
#
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def >: (hash[untyped, untyped] other_hash) -> bool
# <!--
# rdoc-file=hash.c
# - self >= other_hash -> true or false
# -->
# Returns `true` if the entries of `self` are a superset of the entries of
# `other_hash`, `false` otherwise:
#
# h0 = {foo: 0, bar: 1, baz: 2}
# h1 = {foo: 0, bar: 1}
# h0 >= h1 # => true
# h0 >= h0 # => true
# h1 >= h0 # => false
#
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
#
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
# hash.
#
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def >=: (hash[untyped, untyped] other_hash) -> bool
# <!--
# rdoc-file=hash.c
# - self[key] -> object
# -->
# Searches for a hash key equivalent to the given `key`; see [Hash Key
# Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
#
# If the key is found, returns its value:
#
# {foo: 0, bar: 1, baz: 2}
# h[:bar] # => 1
#
# Otherwise, returns a default value (see [Hash
# Default](rdoc-ref:Hash@Hash+Default)).
#
# Related: #[]=; see also [Methods for
# Fetching](rdoc-ref:Hash@Methods+for+Fetching).
#
def []: %a{implicitly-returns-nil} (_Key key) -> V
# <!--
# rdoc-file=hash.c
# - self[key] = object -> object
# -->
# Associates the given `object` with the given `key`; returns `object`.
#
# Searches for a hash key equivalent to the given `key`; see [Hash Key
# Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
#
# If the key is found, replaces its value with the given `object`; the ordering
# is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
#
# h = {foo: 0, bar: 1}
# h[:foo] = 2 # => 2
# h[:foo] # => 2
#
# If `key` is not found, creates a new entry for the given `key` and `object`;
# the new entry is last in the order (see [Entry
# Order](rdoc-ref:Hash@Entry+Order)):
#
# h = {foo: 0, bar: 1}
# h[:baz] = 2 # => 2
# h[:baz] # => 2
# h # => {:foo=>0, :bar=>1, :baz=>2}
#
# Related: #[]; see also [Methods for
# Assigning](rdoc-ref:Hash@Methods+for+Assigning).
#
def []=: (K key, V value) -> V
# <!--
# rdoc-file=hash.c
# - any? -> true or false
# - any?(entry) -> true or false
# - any? {|key, value| ... } -> true or false
# -->
# Returns `true` if any element satisfies a given criterion; `false` otherwise.
#
# If `self` has no element, returns `false` and argument or block are not used;
# otherwise behaves as below.
#
# With no argument and no block, returns `true` if `self` is non-empty, `false`
# otherwise.
#
# With argument `entry` and no block, returns `true` if for any key `key`
# <code>self.assoc(key) == entry</code>, `false` otherwise:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.assoc(:bar) # => [:bar, 1]
# h.any?([:bar, 1]) # => true
# h.any?([:bar, 0]) # => false
#
# With no argument and a block given, calls the block with each key-value pair;
# returns `true` if the block returns a truthy value, `false` otherwise:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.any? {|key, value| value < 3 } # => true
# h.any? {|key, value| value > 3 } # => false
#
# With both argument `entry` and a block given, issues a warning and ignores the
# block.
#
# Related: Enumerable#any? (which this method overrides); see also [Methods for
# Fetching](rdoc-ref:Hash@Methods+for+Fetching).
#
def any?: () -> bool
| (Enumerable::_Pattern pattern) -> bool
| () { ([ K, V ] pair) -> boolish } -> bool
# <!--
# rdoc-file=hash.c
# - assoc(key) -> entry or nil
# -->
# If the given `key` is found, returns its entry as a 2-element array containing
# that key and its value:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.assoc(:bar) # => [:bar, 1]
#
# Returns `nil` if the key is not found.
#
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
#
def assoc: (RBS::Ops::_Equals key) -> [ K, V ]?
# <!--
# rdoc-file=hash.c
# - clear -> self
# -->
# Removes all entries from `self`; returns emptied `self`.
#
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
#
def clear: () -> self
# <!--
# rdoc-file=hash.c
# - compact -> new_hash
# -->
# Returns a copy of `self` with all `nil`-valued entries removed:
#
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
# h.compact # => {foo: 0, baz: 2}
#
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
#
def compact: () -> instance
# <!--
# rdoc-file=hash.c
# - compact! -> self or nil
# -->
# If `self` contains any `nil`-valued entries, returns `self` with all
# `nil`-valued entries removed; returns `nil` otherwise:
#
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
# h.compact!
# h # => {foo: 0, baz: 2}
# h.compact! # => nil
#
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
#
def compact!: () -> self?
# <!--
# rdoc-file=hash.c
# - compare_by_identity -> self
# -->
# Sets `self` to compare keys using *identity* (rather than mere *equality*);
# returns `self`:
#
# By default, two keys are considered to be the same key if and only if they are
# *equal* objects (per method #eql?):
#
# h = {}
# h['x'] = 0
# h['x'] = 1 # Overwrites.
# h # => {"x"=>1}
#
# When this method has been called, two keys are considered to be the same key
# if and only if they are the *same* object:
#
# h.compare_by_identity
# h['x'] = 2 # Does not overwrite.
# h # => {"x"=>1, "x"=>2}
#
# Related: #compare_by_identity?; see also [Methods for
# Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def compare_by_identity: () -> self
# <!--
# rdoc-file=hash.c
# - compare_by_identity? -> true or false
# -->
# Returns whether #compare_by_identity has been called:
#
# h = {}
# h.compare_by_identity? # => false
# h.compare_by_identity
# h.compare_by_identity? # => true
#
# Related: #compare_by_identity; see also [Methods for
# Comparing](rdoc-ref:Hash@Methods+for+Comparing).
#
def compare_by_identity?: () -> bool
# <!--
# rdoc-file=hash.c
# - deconstruct_keys(p1)
# -->
#
def deconstruct_keys: (untyped) -> self
# <!--
# rdoc-file=hash.c
# - default -> object
# - default(key) -> object
# -->
# Returns the default value for the given `key`. The returned value will be
# determined either by the default proc or by the default value. See [Hash
# Default](rdoc-ref:Hash@Hash+Default).
#
# With no argument, returns the current default value:
# h = {}
# h.default # => nil
#
# If `key` is given, returns the default value for `key`, regardless of whether
# that key exists:
# h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
# h[:foo] = "Hello"
# h.default(:foo) # => "No key foo"
#
def default: (?_Key key) -> V?
# <!--
# rdoc-file=hash.c
# - default = value -> object
# -->
# Sets the default value to `value`; returns `value`:
# h = {}
# h.default # => nil
# h.default = false # => false
# h.default # => false
#
# See [Hash Default](rdoc-ref:Hash@Hash+Default).
#
def default=: (V default) -> V
# <!--
# rdoc-file=hash.c
# - default_proc -> proc or nil
# -->
# Returns the default proc for `self` (see [Hash
# Default](rdoc-ref:Hash@Hash+Default)):
# h = {}
# h.default_proc # => nil
# h.default_proc = proc {|hash, key| "Default value for #{key}" }
# h.default_proc.class # => Proc
#
def default_proc: () -> (^(self, _Key key) -> V)?
# <!--
# rdoc-file=hash.c
# - default_proc = proc -> proc
# -->
# Sets the default proc for `self` to `proc` (see [Hash
# Default](rdoc-ref:Hash@Hash+Default)):
# h = {}
# h.default_proc # => nil
# h.default_proc = proc { |hash, key| "Default value for #{key}" }
# h.default_proc.class # => Proc
# h.default_proc = nil
# h.default_proc # => nil
#
def default_proc=: (nil) -> nil
| (^(self, _Key key) -> V proc) -> ^(self, _Key) -> V
| (_ToProc proc) -> Proc
# <!--
# rdoc-file=hash.c
# - delete(key) -> value or nil
# - delete(key) {|key| ... } -> object
# -->
# If an entry for the given `key` is found, deletes the entry and returns its
# associated value; otherwise returns `nil` or calls the given block.
#
# With no block given and `key` found, deletes the entry and returns its value:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.delete(:bar) # => 1
# h # => {foo: 0, baz: 2}
#
# With no block given and `key` not found, returns `nil`.
#
# With a block given and `key` found, ignores the block, deletes the entry, and
# returns its value:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.delete(:baz) { |key| raise 'Will never happen'} # => 2
# h # => {foo: 0, bar: 1}
#
# With a block given and `key` not found, calls the block and returns the
# block's return value:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
# h # => {foo: 0, bar: 1, baz: 2}
#
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
#
def delete: (_Key key) -> V?
| [T, K2 < _Key] (K2 key) { (K2 key) -> T } -> (T | V)
# <!--
# rdoc-file=hash.c
# - delete_if {|key, value| ... } -> self
# - delete_if -> new_enumerator
# -->
# With a block given, calls the block with each key-value pair, deletes each
# entry for which the block returns a truthy value, and returns `self`:
#
# h = {foo: 0, bar: 1, baz: 2}
# h.delete_if {|key, value| value > 0 } # => {foo: 0}
#
# With no block given, returns a new Enumerator.
#
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
#
def delete_if: () -> Enumerator[[ K, V ], self]
| () { (K key, V value) -> boolish } -> self
# <!--
# rdoc-file=hash.c
# - dig(key, *identifiers) -> object
# -->
# Finds and returns an object found in nested objects, as specified by `key` and
# `identifiers`.
#
# The nested objects may be instances of various classes. See [Dig
# Methods](rdoc-ref:dig_methods.rdoc).
#
# Nested hashes:
#
# h = {foo: {bar: {baz: 2}}}
# h.dig(:foo) # => {bar: {baz: 2}}
# h.dig(:foo, :bar) # => {baz: 2}
# h.dig(:foo, :bar, :baz) # => 2
# h.dig(:foo, :bar, :BAZ) # => nil
#
# Nested hashes and arrays:
#