source: EcnlProtoTool/trunk/mruby-1.2.0/mrblib/hash.rb@ 270

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

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 7.8 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 self.clear
158 hash = hash.to_hash
159 hash.each_key{|k|
160 self[k] = hash[k]
161 }
162 if hash.default_proc
163 self.default_proc = hash.default_proc
164 else
165 self.default = hash.default
166 end
167 self
168 end
169 # ISO 15.2.13.4.17
170 alias initialize_copy replace
171
172 ##
173 # Return a hash which contains the content of
174 # +self+ and +other+. If a block is given
175 # it will be called for each element with
176 # a duplicate key. The value of the block
177 # will be the final value of this element.
178 #
179 # ISO 15.2.13.4.22
180 def merge(other, &block)
181 h = {}
182 raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
183 other = other.to_hash
184 self.each_key{|k| h[k] = self[k]}
185 if block
186 other.each_key{|k|
187 h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
188 }
189 else
190 other.each_key{|k| h[k] = other[k]}
191 end
192 h
193 end
194
195 # internal method for Hash inspection
196 def _inspect
197 return "{}" if self.size == 0
198 "{"+self.map {|k,v|
199 k._inspect + "=>" + v._inspect
200 }.join(", ")+"}"
201 end
202 ##
203 # Return the contents of this hash as a string.
204 #
205 # ISO 15.2.13.4.30 (x)
206 def inspect
207 begin
208 self._inspect
209 rescue SystemStackError
210 "{...}"
211 end
212 end
213 # ISO 15.2.13.4.31 (x)
214 alias to_s inspect
215
216 ##
217 # call-seq:
218 # hsh.reject! {| key, value | block } -> hsh or nil
219 # hsh.reject! -> an_enumerator
220 #
221 # Equivalent to <code>Hash#delete_if</code>, but returns
222 # <code>nil</code> if no changes were made.
223 #
224 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
225 #
226 def reject!(&b)
227 return to_enum :reject! unless block_given?
228
229 keys = []
230 self.each{|k,v|
231 if b.call([k, v])
232 keys.push(k)
233 end
234 }
235 return nil if keys.size == 0
236 keys.each{|k|
237 self.delete(k)
238 }
239 self
240 end
241
242 ##
243 # call-seq:
244 # hsh.reject {|key, value| block} -> a_hash
245 # hsh.reject -> an_enumerator
246 #
247 # Returns a new hash consisting of entries for which the block returns false.
248 #
249 # If no block is given, an enumerator is returned instead.
250 #
251 # h = { "a" => 100, "b" => 200, "c" => 300 }
252 # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
253 # h.reject {|k,v| v > 100} #=> {"a" => 100}
254 #
255 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing.
256 #
257 def reject(&b)
258 return to_enum :reject unless block_given?
259
260 h = {}
261 self.each{|k,v|
262 unless b.call([k, v])
263 h[k] = v
264 end
265 }
266 h
267 end
268
269 ##
270 # call-seq:
271 # hsh.select! {| key, value | block } -> hsh or nil
272 # hsh.select! -> an_enumerator
273 #
274 # Equivalent to <code>Hash#keep_if</code>, but returns
275 # <code>nil</code> if no changes were made.
276 #
277 # 1.9 Hash#select! returns Hash; ISO says nothing.
278 #
279 def select!(&b)
280 return to_enum :select! unless block_given?
281
282 keys = []
283 self.each{|k,v|
284 unless b.call([k, v])
285 keys.push(k)
286 end
287 }
288 return nil if keys.size == 0
289 keys.each{|k|
290 self.delete(k)
291 }
292 self
293 end
294
295 ##
296 # call-seq:
297 # hsh.select {|key, value| block} -> a_hash
298 # hsh.select -> an_enumerator
299 #
300 # Returns a new hash consisting of entries for which the block returns true.
301 #
302 # If no block is given, an enumerator is returned instead.
303 #
304 # h = { "a" => 100, "b" => 200, "c" => 300 }
305 # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
306 # h.select {|k,v| v < 200} #=> {"a" => 100}
307 #
308 # 1.9 Hash#select returns Hash; ISO says nothing
309 #
310 def select(&b)
311 return to_enum :select unless block_given?
312
313 h = {}
314 self.each{|k,v|
315 if b.call([k, v])
316 h[k] = v
317 end
318 }
319 h
320 end
321
322 ##
323 # call-seq:
324 # hsh.rehash -> hsh
325 #
326 # Rebuilds the hash based on the current hash values for each key. If
327 # values of key objects have changed since they were inserted, this
328 # method will reindex <i>hsh</i>.
329 #
330 # h = {"AAA" => "b"}
331 # h.keys[0].chop!
332 # h #=> {"AA"=>"b"}
333 # h["AA"] #=> nil
334 # h.rehash #=> {"AA"=>"b"}
335 # h["AA"] #=> "b"
336 #
337 def rehash
338 h = {}
339 self.each{|k,v|
340 h[k] = v
341 }
342 self.replace(h)
343 end
344
345 def __update(h)
346 h.each_key{|k| self[k] = h[k]}
347 self
348 end
349end
350
351##
352# Hash is enumerable
353#
354# ISO 15.2.13.3
355class Hash
356 include Enumerable
357end
Note: See TracBrowser for help on using the repository browser.