diff --git a/core/hash.rbs b/core/hash.rbs new file mode 100644 index 0000000..bf25e91 --- /dev/null +++ b/core/hash.rbs @@ -0,0 +1,45 @@ +class Hash[unchecked out K, unchecked out V] < Object + include Enumerable[[K, V]] + + def initialize: () -> void + + def []: %a{implicitly-returns-nil} (K key) -> V + + def []=: (K key, V value) -> V + + def clear: () -> self + + def dup: () -> Hash[K, V] + + def delete: (K key) -> V? + + def empty?: () -> bool + + def has_key?: (K key) -> bool + + def has_value?: (V value) -> bool + + def key: (V value) -> K? + + def keys: () -> Array[K] + + def size: () -> Integer + + alias length size + + alias count size + + def merge: [U, W](Hash[U, W] other) -> Hash[K | U, V | W] + + def merge!: (Hash[K, V] other) -> self + + def values: () -> Array[V] + + def inspect: () -> String + def self.inspect: () -> String + + alias to_s inspect + alias self.to_s self.inspect + + def each: () { ([K, V]) -> void } -> self +end diff --git a/test/hash.rb b/test/hash.rb new file mode 100644 index 0000000..67c2f6a --- /dev/null +++ b/test/hash.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +# rubocop:disable Lint/Void +# rubocop:disable Style/EmptyLiteral +# rubocop:disable Style/PreferredHashMethods + +hash = { a: 1, b: 2, c: 3 } + +Hash.new + +hash[:a] + +hash[:a] = 2 + +hash.clear + +hash.dup + +hash.delete :a + +hash.empty? + +hash.has_key? :a + +hash.has_value? 0 + +hash.key 2 + +hash.keys + +hash.size + +hash.length + +hash.count + +hash.merge({}) +hash.merge({ 'a' => true, 5 => '' }) + +hash.merge!({ d: 4 }) + +hash.values + +hash.inspect +Hash.inspect + +hash.to_s +Hash.to_s + +hash.each do |key, value| + key + value +end + +# rubocop:enable all