Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions core/hash.rbs
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions test/hash.rb
Original file line number Diff line number Diff line change
@@ -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