source: EcnlProtoTool/trunk/mruby-1.3.0/mrblib/hash.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 7.9 KB
Line 
1##
2# Hash
3#
4# ISO 15.2.13
5class Hash
6 ##
7 # Equality---Two hashes are equal if they each contain the same number
8 # of keys and if each key-value pair is equal to (according to
9 # <code>Object#==</code>) the corresponding elements in the other
10 # hash.
11 #
12 # ISO 15.2.13.4.1
13 def ==(hash)
14 return true if self.equal?(hash)
15 begin
16 hash = hash.to_hash
17 rescue NoMethodError
18 return false
19 end
20 return false if self.size != hash.size
21 self.each do |k,v|
22 return false unless hash.key?(k)
23 return false unless self[k] == hash[k]
24 end
25 return true
26 end
27
28 ##
29 # Returns <code>true</code> if <i>hash</i> and <i>other</i> are
30 # both hashes with the same content compared by eql?.
31 #
32 # ISO 15.2.13.4.32 (x)
33 def eql?(hash)
34 return true if self.equal?(hash)
35 begin
36 hash = hash.to_hash
37 rescue NoMethodError
38 return false
39 end
40 return false if self.size != hash.size
41 self.each do |k,v|
42 return false unless hash.key?(k)
43 return false unless self[k].eql?(hash[k])
44 end
45 return true
46 end
47
48 ##
49 # Delete the element with the key +key+.
50 # Return the value of the element if +key+
51 # was found. Return nil if nothing was
52 # found. If a block is given, call the
53 # block with the value of the element.
54 #
55 # ISO 15.2.13.4.8
56 def delete(key, &block)
57 if block && !self.has_key?(key)
58 block.call(key)
59 else
60 self.__delete(key)
61 end
62 end
63
64 ##
65 # Calls the given block for each element of +self+
66 # and pass the key and value of each element.
67 #
68 # call-seq:
69 # hsh.each {| key, value | block } -> hsh
70 # hsh.each_pair {| key, value | block } -> hsh
71 # hsh.each -> an_enumerator
72 # hsh.each_pair -> an_enumerator
73 #
74 #
75 # If no block is given, an enumerator is returned instead.
76 #
77 # h = { "a" => 100, "b" => 200 }
78 # h.each {|key, value| puts "#{key} is #{value}" }
79 #
80 # <em>produces:</em>
81 #
82 # a is 100
83 # b is 200
84 #
85 # ISO 15.2.13.4.9
86 def each(&block)
87 return to_enum :each unless block_given?
88
89 keys = self.keys
90 vals = self.values
91 len = self.size
92 i = 0
93 while i < len
94 block.call [keys[i], vals[i]]
95 i += 1
96 end
97 self
98 end
99
100 ##
101 # Calls the given block for each element of +self+
102 # and pass the key of each element.
103 #
104 # call-seq:
105 # hsh.each_key {| key | block } -> hsh
106 # hsh.each_key -> an_enumerator
107 #
108 # If no block is given, an enumerator is returned instead.
109 #
110 # h = { "a" => 100, "b" => 200 }
111 # h.each_key {|key| puts key }
112 #
113 # <em>produces:</em>
114 #
115 # a
116 # b
117 #
118 # ISO 15.2.13.4.10
119 def each_key(&block)
120 return to_enum :each_key unless block_given?
121
122 self.keys.each{|k| block.call(k)}
123 self
124 end
125
126 ##
127 # Calls the given block for each element of +self+
128 # and pass the value of each element.
129 #
130 # call-seq:
131 # hsh.each_value {| value | block } -> hsh
132 # hsh.each_value -> an_enumerator
133 #
134 # If no block is given, an enumerator is returned instead.
135 #
136 # h = { "a" => 100, "b" => 200 }
137 # h.each_value {|value| puts value }
138 #
139 # <em>produces:</em>
140 #
141 # 100
142 # 200
143 #
144 # ISO 15.2.13.4.11
145 def each_value(&block)
146 return to_enum :each_value unless block_given?
147
148 self.keys.each{|k| block.call(self[k])}
149 self
150 end
151
152 ##
153 # Replaces the contents of <i>hsh</i> with the contents of other hash
154 #
155 # ISO 15.2.13.4.23
156 def replace(hash)
157 raise TypeError, "can't convert argument into Hash" unless hash.respond_to?(:to_hash)
158 self.clear
159 hash = hash.to_hash
160 hash.each_key{|k|
161 self[k] = hash[k]
162 }
163 if hash.default_proc
164 self.default_proc = hash.default_proc
165 else
166 self.default = hash.default
167 end
168 self
169 end
170 # ISO 15.2.13.4.17
171 alias initialize_copy replace
172
173 ##
174 # Return a hash which contains the content of
175 # +self+ and +other+. If a block is given
176 # it will be called for each element with
177 # a duplicate key. The value of the block
178 # will be the final value of this element.
179 #
180 # ISO 15.2.13.4.22
181 def merge(other, &block)
182 h = {}
183 raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
184 other = other.to_hash
185 self.each_key{|k| h[k] = self[k]}
186 if block
187 other.each_key{|k|
188 h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
189 }
190 else
191 other.each_key{|k| h[k] = other[k]}
192 end
193 h
194 end
195
196 # internal method for Hash inspection
197 def _inspect
198 return "{}" if self.size == 0
199 "{"+self.map {|k,v|
200 k._inspect + "=>" + v._inspect
201 }.join(", ")+"}"
202 end
203 ##
204 # Return the contents of this hash as a string.
205 #
206 # ISO 15.2.13.4.30 (x)
207 def inspect
208 begin
209 self._inspect
210 rescue SystemStackError
211 "{...}"
212 end
213 end
214 # ISO 15.2.13.4.31 (x)
215 alias to_s inspect
216
217 ##
218 # call-seq:
219 # hsh.reject! {| key, value | block } -> hsh or nil
220 # hsh.reject! -> an_enumerator
221 #
222 # Equivalent to <code>Hash#delete_if</code>, but returns
223 # <code>nil</code> if no changes were made.
224 #
225 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
226 #
227 def reject!(&b)
228 return to_enum :reject! unless block_given?
229
230 keys = []
231 self.each{|k,v|
232 if b.call([k, v])
233 keys.push(k)
234 end
235 }
236 return nil if keys.size == 0
237 keys.each{|k|
238 self.delete(k)
239 }
240 self
241 end
242
243 ##
244 # call-seq:
245 # hsh.reject {|key, value| block} -> a_hash
246 # hsh.reject -> an_enumerator
247 #
248 # Returns a new hash consisting of entries for which the block returns false.
249 #
250 # If no block is given, an enumerator is returned instead.
251 #
252 # h = { "a" => 100, "b" => 200, "c" => 300 }
253 # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
254 # h.reject {|k,v| v > 100} #=> {"a" => 100}
255 #
256 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing.
257 #
258 def reject(&b)
259 return to_enum :reject unless block_given?
260
261 h = {}
262 self.each{|k,v|
263 unless b.call([k, v])
264 h[k] = v
265 end
266 }
267 h
268 end
269
270 ##
271 # call-seq:
272 # hsh.select! {| key, value | block } -> hsh or nil
273 # hsh.select! -> an_enumerator
274 #
275 # Equivalent to <code>Hash#keep_if</code>, but returns
276 # <code>nil</code> if no changes were made.
277 #
278 # 1.9 Hash#select! returns Hash; ISO says nothing.
279 #
280 def select!(&b)
281 return to_enum :select! unless block_given?
282
283 keys = []
284 self.each{|k,v|
285 unless b.call([k, v])
286 keys.push(k)
287 end
288 }
289 return nil if keys.size == 0
290 keys.each{|k|
291 self.delete(k)
292 }
293 self
294 end
295
296 ##
297 # call-seq:
298 # hsh.select {|key, value| block} -> a_hash
299 # hsh.select -> an_enumerator
300 #
301 # Returns a new hash consisting of entries for which the block returns true.
302 #
303 # If no block is given, an enumerator is returned instead.
304 #
305 # h = { "a" => 100, "b" => 200, "c" => 300 }
306 # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
307 # h.select {|k,v| v < 200} #=> {"a" => 100}
308 #
309 # 1.9 Hash#select returns Hash; ISO says nothing
310 #
311 def select(&b)
312 return to_enum :select unless block_given?
313
314 h = {}
315 self.each{|k,v|
316 if b.call([k, v])
317 h[k] = v
318 end
319 }
320 h
321 end
322
323 ##
324 # call-seq:
325 # hsh.rehash -> hsh
326 #
327 # Rebuilds the hash based on the current hash values for each key. If
328 # values of key objects have changed since they were inserted, this
329 # method will reindex <i>hsh</i>.
330 #
331 # h = {"AAA" => "b"}
332 # h.keys[0].chop!
333 # h #=> {"AA"=>"b"}
334 # h["AA"] #=> nil
335 # h.rehash #=> {"AA"=>"b"}
336 # h["AA"] #=> "b"
337 #
338 def rehash
339 h = {}
340 self.each{|k,v|
341 h[k] = v
342 }
343 self.replace(h)
344 end
345
346 def __update(h)
347 h.each_key{|k| self[k] = h[k]}
348 self
349 end
350end
351
352##
353# Hash is enumerable
354#
355# ISO 15.2.13.3
356class Hash
357 include Enumerable
358end
Note: See TracBrowser for help on using the repository browser.