Debian Codegraph @

vendor/mruby-source/mrblib/hash.rb

raw
1 ##
2 # Hash
3 #
4 # ISO 15.2.13
5 class Hash
6 ##
7 # Hash is enumerable
8 #
9 # ISO 15.2.13.3
10 include Enumerable
11
12 ##
13 # call-seq:
14 # hash == object -> true or false
15 #
16 # Equality---Two hashes are equal if they each contain the same number
17 # of keys and if each key-value pair is equal to (according to
18 # <code>Object#==</code>) the corresponding elements in the other
19 # hash.
20 #
21 # ISO 15.2.13.4.1
22 def ==(hash)
23 return true if self.equal?(hash)
24 unless Hash === hash
25 return false
26 end
27 return false if self.size != hash.size
28 self.each do |k,v|
29 return false unless hash.key?(k)
30 return false unless self[k] == hash[k]
31 end
32 return true
33 end
34
35 ##
36 # call-seq:
37 # hash.eql? object -> true or false
38 #
39 # Returns <code>true</code> if <i>hash</i> and <i>other</i> are
40 # both hashes with the same content compared by eql?.
41 #
42 def eql?(hash)
43 return true if self.equal?(hash)
44 unless Hash === hash
45 return false
46 end
47 return false if self.size != hash.size
48 self.each do |k,v|
49 return false unless hash.key?(k)
50 return false unless self[k].eql?(hash[k])
51 end
52 return true
53 end
54
55 ##
56 # call-seq:
57 # hash.delete(key) -> value or nil
58 # hash.delete(key) {|key| ... } -> object
59 #
60 # Delete the element with the key +key+.
61 # Return the value of the element if +key+
62 # was found. Return nil if nothing was
63 # found. If a block is given, call the
64 # block with the value of the element.
65 #
66 # ISO 15.2.13.4.8
67 def delete(key, &block)
68 if block && !self.has_key?(key)
69 return block.call(key)
70 end
71 self.__delete(key)
72 end
73
74 ##
75 # call-seq:
76 # hsh.each {| key, value | block } -> hsh
77 # hsh.each_pair {| key, value | block } -> hsh
78 # hsh.each -> an_enumerator
79 # hsh.each_pair -> an_enumerator
80 #
81 # Calls the given block for each element of +self+
82 # and pass the key and value of each element.
83 #
84 # If no block is given, an enumerator is returned instead.
85 #
86 # h = { "a" => 100, "b" => 200 }
87 # h.each {|key, value| puts "#{key} is #{value}" }
88 #
89 # <em>produces:</em>
90 #
91 # a is 100
92 # b is 200
93 #
94 # ISO 15.2.13.4.9
95 def each(&block)
96 return to_enum :each unless block
97
98 keys = self.keys
99 vals = self.values
100 len = self.size
101 i = 0
102 while i < len
103 block.call [keys[i], vals[i]]
104 i += 1
105 end
106 self
107 end
108
109 ##
110 # call-seq:
111 # hsh.each_key {| key | block } -> hsh
112 # hsh.each_key -> an_enumerator
113 #
114 # Calls the given block for each element of +self+
115 # and pass the key of each element.
116 #
117 # If no block is given, an enumerator is returned instead.
118 #
119 # h = { "a" => 100, "b" => 200 }
120 # h.each_key {|key| puts key }
121 #
122 # <em>produces:</em>
123 #
124 # a
125 # b
126 #
127 # ISO 15.2.13.4.10
128 def each_key(&block)
129 return to_enum :each_key unless block
130
131 self.keys.each{|k| block.call(k)}
132 self
133 end
134
135 ##
136 # call-seq:
137 # hsh.each_value {| value | block } -> self
138 # hsh.each_value -> an_enumerator
139 #
140 # Calls the given block with each value; returns +self+:
141 #
142 # If no block is given, an enumerator is returned instead.
143 #
144 # h = { "a" => 100, "b" => 200 }
145 # h.each_value {|value| puts value }
146 #
147 # <em>produces:</em>
148 #
149 # 100
150 # 200
151 #
152 # ISO 15.2.13.4.11
153 def each_value(&block)
154 return to_enum :each_value unless block
155
156 self.values.each{|v| block.call(v)}
157 self
158 end
159
160 ##
161 # call-seq:
162 # hsh.merge(other_hash..) -> hsh
163 # hsh.merge(other_hash..){|key, oldval, newval| block} -> hsh
164 #
165 # Returns the new \Hash formed by merging each of +other_hashes+
166 # into a copy of +self+.
167 #
168 # Each argument in +other_hashes+ must be a \Hash.
169 # Adds the contents of _other_hash_ to _hsh_. If no block is specified,
170 # entries with duplicate keys are overwritten with the values from
171 # _other_hash_, otherwise the value of each duplicate key is determined by
172 # calling the block with the key, its value in _hsh_ and its value in
173 # _other_hash_.
174 #
175 # Example:
176 # h = {foo: 0, bar: 1, baz: 2}
177 # h1 = {bat: 3, bar: 4}
178 # h2 = {bam: 5, bat:6}
179 # h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
180 # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
181 #
182 # ISO 15.2.13.4.22
183 def merge(*others, &block)
184 h = self.dup
185 return h.__merge(*others) unless block
186 i=0; len=others.size
187 while i<len
188 other = others[i]
189 i += 1
190 raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
191 other.each_key{|k|
192 h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
193 }
194 end
195 h
196 end
197
198 ##
199 # call-seq:
200 # hsh.reject! {| key, value | block } -> hsh or nil
201 # hsh.reject! -> an_enumerator
202 #
203 # Equivalent to <code>Hash#delete_if</code>, but returns
204 # <code>nil</code> if no changes were made.
205 #
206 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
207 #
208 def reject!(&block)
209 return to_enum :reject! unless block
210
211 keys = []
212 self.each{|k,v|
213 if block.call([k, v])
214 keys.push(k)
215 end
216 }
217 return nil if keys.size == 0
218 keys.each{|k|
219 self.delete(k)
220 }
221 self
222 end
223
224 ##
225 # call-seq:
226 # hsh.reject {|key, value| block} -> a_hash
227 # hsh.reject -> an_enumerator
228 #
229 # Returns a new hash consisting of entries for which the block returns false.
230 #
231 # If no block is given, an enumerator is returned instead.
232 #
233 # h = { "a" => 100, "b" => 200, "c" => 300 }
234 # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
235 # h.reject {|k,v| v > 100} #=> {"a" => 100}
236 #
237 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing.
238 #
239 def reject(&block)
240 return to_enum :reject unless block
241
242 h = {}
243 self.each{|k,v|
244 unless block.call([k, v])
245 h[k] = v
246 end
247 }
248 h
249 end
250
251 ##
252 # call-seq:
253 # hsh.select! {| key, value | block } -> hsh or nil
254 # hsh.select! -> an_enumerator
255 #
256 # Equivalent to <code>Hash#keep_if</code>, but returns
257 # <code>nil</code> if no changes were made.
258 #
259 # 1.9 Hash#select! returns Hash; ISO says nothing.
260 #
261 def select!(&block)
262 return to_enum :select! unless block
263
264 keys = []
265 self.each{|k,v|
266 unless block.call([k, v])
267 keys.push(k)
268 end
269 }
270 return nil if keys.size == 0
271 keys.each{|k|
272 self.delete(k)
273 }
274 self
275 end
276
277 ##
278 # call-seq:
279 # hsh.select {|key, value| block} -> a_hash
280 # hsh.select -> an_enumerator
281 #
282 # Returns a new hash consisting of entries for which the block returns true.
283 #
284 # If no block is given, an enumerator is returned instead.
285 #
286 # h = { "a" => 100, "b" => 200, "c" => 300 }
287 # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
288 # h.select {|k,v| v < 200} #=> {"a" => 100}
289 #
290 # 1.9 Hash#select returns Hash; ISO says nothing
291 #
292 def select(&block)
293 return to_enum :select unless block
294
295 h = {}
296 self.each{|k,v|
297 if block.call([k, v])
298 h[k] = v
299 end
300 }
301 h
302 end
303 end