This repository was archived by the owner on Mar 15, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +60
-0
lines changed Expand file tree Collapse file tree 4 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,20 @@ def merge!(other_hash)
8888 end
8989 end
9090
91+ # The number of entries in the map
92+ def size
93+ @references_to_keys_map . count do |rkey , ref |
94+ ref . object
95+ end
96+ end
97+
98+ alias_method :length , :size
99+
100+ # True if there are entries that exist in the map
101+ def empty?
102+ size == 0
103+ end
104+
91105 def inspect
92106 live_entries = { }
93107 each do |key , value |
Original file line number Diff line number Diff line change @@ -102,6 +102,20 @@ def merge!(other_hash)
102102 end
103103 end
104104
105+ # The number of entries in the map
106+ def size
107+ @references . count do |key , ref |
108+ ref . object
109+ end
110+ end
111+
112+ alias_method :length , :size
113+
114+ # True if there are entries that exist in the map
115+ def empty?
116+ size == 0
117+ end
118+
105119 def inspect
106120 live_entries = { }
107121 each do |key , value |
Original file line number Diff line number Diff line change @@ -145,6 +145,21 @@ def test_can_iterate_over_all_entries
145145 end
146146 end
147147
148+ def test_size
149+ Ref ::Mock . use do
150+ hash = map_class . new
151+ assert_true hash . empty?
152+ assert_equal 0 , hash . size
153+ key_2 = Object . new
154+ hash [ key_1 ] = "value 1"
155+ assert_false hash . empty?
156+ assert_equal 2 , hash . size
157+ Ref ::Mock . gc ( key_2 )
158+ assert_false hash . empty?
159+ assert_equal 1 , hash . size
160+ end
161+ end
162+
148163 def test_inspect
149164 Ref ::Mock . use do
150165 hash = map_class . new
Original file line number Diff line number Diff line change @@ -125,6 +125,23 @@ def test_can_iterate_over_all_entries
125125 end
126126 end
127127
128+ def test_size
129+ Ref ::Mock . use do
130+ hash = map_class . new
131+ assert_true hash . empty?
132+ assert_equal 0 , hash . size
133+ value_1 = "value 1"
134+ value_2 = "value 2"
135+ hash [ "key 1" ] = value_1
136+ hash [ "key 2" ] = value_2
137+ assert_false hash . empty?
138+ assert_equal 2 , hash . size
139+ Ref ::Mock . gc ( value_2 )
140+ assert_false hash . empty?
141+ assert_equal 1 , hash . size
142+ end
143+ end
144+
128145 def test_inspect
129146 Ref ::Mock . use do
130147 hash = map_class . new
You can’t perform that action at this time.
0 commit comments