diff --git a/core/hash.rbs b/core/hash.rbs index df4bbe6c8..290fb5466 100644 --- a/core/hash.rbs +++ b/core/hash.rbs @@ -486,15 +486,29 @@ # * #flatten: Returns an array that is a 1-dimensional flattening of `self`. # * #invert: Returns a hash with the each key-value pair inverted. # -class Hash[unchecked out K, unchecked out V] < Object +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 + + # Interface for comparing equality of types. + # + interface _Equals + def ==: (untyped other) -> boolish + end + # # - def deconstruct_keys: (Array[K] | nil) -> self + def deconstruct_keys: (untyped) -> self # # Calls the given block with each key-value pair; returns `self`: @@ -937,8 +953,8 @@ class Hash[unchecked out K, unchecked out V] < Object # bar: 1 # baz: 2 # - def each: () { ([ K, V ] arg0) -> untyped } -> self - | () -> ::Enumerator[[ K, V ], self] + def each: () -> Enumerator[[ K, V ], self] + | () { ([ K, V ] pair) -> void } -> self # + # # Returns a new `Hash` object whose entries are those for which the block # returns a truthy value: # h = {foo: 0, bar: 1, baz: 2} @@ -1126,10 +1146,14 @@ class Hash[unchecked out K, unchecked out V] < Object # e = h.select # => #0, :bar=>1, :baz=>2}:select> # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1} # - def filter: () { (K, V) -> boolish } -> ::Hash[K, V] - | () -> ::Enumerator[[ K, V ], ::Hash[K, V]] + def select: () -> Enumerator[[ K, V ], Hash[K, V]] + | () { (K key, V value) -> boolish } -> Hash[K, V] - # + # # Returns `self`, whose entries are those for which the block returns a truthy # value: # h = {foo: 0, bar: 1, baz: 2} @@ -1142,8 +1166,8 @@ class Hash[unchecked out K, unchecked out V] < Object # e = h.select! # => #0, :bar=>1, :baz=>2}:select!> # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1} # - def filter!: () { (K, V) -> boolish } -> self? - | () -> ::Enumerator[[ K, V ], self?] + def select!: () -> Enumerator[[ K, V ], self?] + | () { (K key, V value) -> boolish } -> self? # # Returns `true` if `key` is a key in `self`, otherwise `false`. # - def has_key?: (K arg0) -> bool + def has_key?: (_Key key) -> bool # # Returns `true` if `value` is a value in `self`, otherwise `false`. # - def has_value?: (V arg0) -> bool + def has_value?: (_Equals value) -> bool # # Returns `true` if `key` is a key in `self`, otherwise `false`. @@ -1291,14 +1315,18 @@ class Hash[unchecked out K, unchecked out V] < Object # h = {foo: 0, bar: 1, baz: 2} # h.keys # => [:foo, :bar, :baz] # - def keys: () -> ::Array[K] + def keys: () -> Array[K] - # + # # Returns the count of entries in `self`: # # {foo: 0, bar: 1, baz: 2}.length # => 3 # - def length: () -> Integer + def size: () -> Integer # # Returns `true` if `key` is a key in `self`, otherwise `false`. @@ -1356,8 +1384,8 @@ class Hash[unchecked out K, unchecked out V] < Object # h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' } # h1 # => {:foo=>0, :bar=>1, :baz=>2} # - def merge: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V] - | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> C } -> ::Hash[A | K, B | V | C] + def merge: [A, B] (*hash[A, B] other_hashes) -> Hash[A | K, B | V] + | [A, B, C] (*hash[A, B] other_hashes) { (K key, V oldval, B newval) -> C } -> Hash[A | K, B | V | C] # # Merges each of `other_hashes` into `self`; returns `self`. @@ -1401,8 +1429,8 @@ class Hash[unchecked out K, unchecked out V] < Object # h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' } # h1 # => {:foo=>0, :bar=>1, :baz=>2} # - def merge!: (*::Hash[K, V] other_hashes) -> self - | (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self + def merge!: (*hash[K, V] other_hashes) -> self + | (*hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self # # Replaces the entire contents of `self` with the contents of `other_hash`; @@ -1477,13 +1507,9 @@ class Hash[unchecked out K, unchecked out V] < Object # h = {foo: 0, bar: 1, baz: 2} # h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4} # - def replace: (Hash[K, V]) -> self + def replace: (hash[K, V] other) -> self - # + # # Returns a new `Hash` object whose entries are those for which the block # returns a truthy value: # h = {foo: 0, bar: 1, baz: 2} @@ -1494,13 +1520,9 @@ class Hash[unchecked out K, unchecked out V] < Object # e = h.select # => #0, :bar=>1, :baz=>2}:select> # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1} # - alias select filter + alias filter select - # + # # Returns `self`, whose entries are those for which the block returns a truthy # value: # h = {foo: 0, bar: 1, baz: 2} @@ -1513,7 +1535,7 @@ class Hash[unchecked out K, unchecked out V] < Object # e = h.select! # => #0, :bar=>1, :baz=>2}:select!> # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1} # - alias select! filter! + alias filter! select! # + # # Returns the count of entries in `self`: # # {foo: 0, bar: 1, baz: 2}.length # => 3 # - alias size length + alias length size # # Associates the given `value` with the given `key`; returns `value`. @@ -1580,7 +1598,7 @@ class Hash[unchecked out K, unchecked out V] < Object # h = {foo: 0, bar: 1, baz: 2} # h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]] # - def to_a: () -> ::Array[[ K, V ]] + def to_a: () -> Array[[ K, V ]] #