Skip to content

Commit 72aa743

Browse files
authored
feat: Hash を定義 (#47)
* feat: `Hash` を定義 * chore: テストを追加 * chore: rubocop
1 parent 63cc28b commit 72aa743

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

core/hash.rbs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Hash[unchecked out K, unchecked out V] < Object
2+
include Enumerable[[K, V]]
3+
4+
def initialize: () -> void
5+
6+
def []: %a{implicitly-returns-nil} (K key) -> V
7+
8+
def []=: (K key, V value) -> V
9+
10+
def clear: () -> self
11+
12+
def dup: () -> Hash[K, V]
13+
14+
def delete: (K key) -> V?
15+
16+
def empty?: () -> bool
17+
18+
def has_key?: (K key) -> bool
19+
20+
def has_value?: (V value) -> bool
21+
22+
def key: (V value) -> K?
23+
24+
def keys: () -> Array[K]
25+
26+
def size: () -> Integer
27+
28+
alias length size
29+
30+
alias count size
31+
32+
def merge: [U, W](Hash[U, W] other) -> Hash[K | U, V | W]
33+
34+
def merge!: (Hash[K, V] other) -> self
35+
36+
def values: () -> Array[V]
37+
38+
def inspect: () -> String
39+
def self.inspect: () -> String
40+
41+
alias to_s inspect
42+
alias self.to_s self.inspect
43+
44+
def each: () { ([K, V]) -> void } -> self
45+
end

test/hash.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
# rubocop:disable Lint/Void
4+
# rubocop:disable Style/EmptyLiteral
5+
# rubocop:disable Style/PreferredHashMethods
6+
7+
hash = { a: 1, b: 2, c: 3 }
8+
9+
Hash.new
10+
11+
hash[:a]
12+
13+
hash[:a] = 2
14+
15+
hash.clear
16+
17+
hash.dup
18+
19+
hash.delete :a
20+
21+
hash.empty?
22+
23+
hash.has_key? :a
24+
25+
hash.has_value? 0
26+
27+
hash.key 2
28+
29+
hash.keys
30+
31+
hash.size
32+
33+
hash.length
34+
35+
hash.count
36+
37+
hash.merge({})
38+
hash.merge({ 'a' => true, 5 => '' })
39+
40+
hash.merge!({ d: 4 })
41+
42+
hash.values
43+
44+
hash.inspect
45+
Hash.inspect
46+
47+
hash.to_s
48+
Hash.to_s
49+
50+
hash.each do |key, value|
51+
key
52+
value
53+
end
54+
55+
# rubocop:enable all

0 commit comments

Comments
 (0)