Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrblib/hash.rb
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/hash.rb
r331 r439 13 13 def ==(hash) 14 14 return true if self.equal?(hash) 15 begin 16 hash = hash.to_hash 17 rescue NoMethodError 15 unless Hash === hash 18 16 return false 19 17 end … … 33 31 def eql?(hash) 34 32 return true if self.equal?(hash) 35 begin 36 hash = hash.to_hash 37 rescue NoMethodError 33 unless Hash === hash 38 34 return false 39 35 end … … 56 52 def delete(key, &block) 57 53 if block && !self.has_key?(key) 58 block.call(key) 59 else 60 self.__delete(key) 61 end 54 return block.call(key) 55 end 56 self.__delete(key) 62 57 end 63 58 … … 85 80 # ISO 15.2.13.4.9 86 81 def each(&block) 87 return to_enum :each unless block _given?82 return to_enum :each unless block 88 83 89 84 keys = self.keys … … 118 113 # ISO 15.2.13.4.10 119 114 def each_key(&block) 120 return to_enum :each_key unless block _given?115 return to_enum :each_key unless block 121 116 122 117 self.keys.each{|k| block.call(k)} … … 144 139 # ISO 15.2.13.4.11 145 140 def each_value(&block) 146 return to_enum :each_value unless block _given?141 return to_enum :each_value unless block 147 142 148 143 self.keys.each{|k| block.call(self[k])} … … 155 150 # ISO 15.2.13.4.23 156 151 def replace(hash) 157 raise TypeError, " can't convert argument into Hash" unless hash.respond_to?(:to_hash)152 raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash 158 153 self.clear 159 hash = hash.to_hash160 154 hash.each_key{|k| 161 155 self[k] = hash[k] … … 180 174 # ISO 15.2.13.4.22 181 175 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]} 176 raise TypeError, "Hash required (#{other.class} given)" unless Hash === other 177 h = self.dup 186 178 if block 187 179 other.each_key{|k| … … 195 187 196 188 # internal method for Hash inspection 197 def _inspect 189 def _inspect(recur_list) 198 190 return "{}" if self.size == 0 199 "{"+self.map {|k,v| 200 k._inspect + "=>" + v._inspect 201 }.join(", ")+"}" 191 return "{...}" if recur_list[self.object_id] 192 recur_list[self.object_id] = true 193 ary=[] 194 keys=self.keys 195 size=keys.size 196 i=0 197 while i<size 198 k=keys[i] 199 ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list)) 200 i+=1 201 end 202 "{"+ary.join(", ")+"}" 202 203 end 203 204 ## … … 206 207 # ISO 15.2.13.4.30 (x) 207 208 def inspect 208 begin 209 self._inspect 210 rescue SystemStackError 211 "{...}" 212 end 209 self._inspect({}) 213 210 end 214 211 # ISO 15.2.13.4.31 (x) … … 225 222 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. 226 223 # 227 def reject!(&b )228 return to_enum :reject! unless block _given?224 def reject!(&block) 225 return to_enum :reject! unless block 229 226 230 227 keys = [] 231 228 self.each{|k,v| 232 if b .call([k, v])229 if block.call([k, v]) 233 230 keys.push(k) 234 231 end … … 256 253 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing. 257 254 # 258 def reject(&b )259 return to_enum :reject unless block _given?255 def reject(&block) 256 return to_enum :reject unless block 260 257 261 258 h = {} 262 259 self.each{|k,v| 263 unless b .call([k, v])260 unless block.call([k, v]) 264 261 h[k] = v 265 262 end … … 278 275 # 1.9 Hash#select! returns Hash; ISO says nothing. 279 276 # 280 def select!(&b )281 return to_enum :select! unless block _given?277 def select!(&block) 278 return to_enum :select! unless block 282 279 283 280 keys = [] 284 281 self.each{|k,v| 285 unless b .call([k, v])282 unless block.call([k, v]) 286 283 keys.push(k) 287 284 end … … 309 306 # 1.9 Hash#select returns Hash; ISO says nothing 310 307 # 311 def select(&b )312 return to_enum :select unless block _given?308 def select(&block) 309 return to_enum :select unless block 313 310 314 311 h = {} 315 312 self.each{|k,v| 316 if b .call([k, v])313 if block.call([k, v]) 317 314 h[k] = v 318 315 end 319 316 } 320 317 h 321 end322 323 ##324 # call-seq:325 # hsh.rehash -> hsh326 #327 # Rebuilds the hash based on the current hash values for each key. If328 # values of key objects have changed since they were inserted, this329 # method will reindex <i>hsh</i>.330 #331 # h = {"AAA" => "b"}332 # h.keys[0].chop!333 # h #=> {"AA"=>"b"}334 # h["AA"] #=> nil335 # h.rehash #=> {"AA"=>"b"}336 # h["AA"] #=> "b"337 #338 def rehash339 h = {}340 self.each{|k,v|341 h[k] = v342 }343 self.replace(h)344 end345 346 def __update(h)347 h.each_key{|k| self[k] = h[k]}348 self349 318 end 350 319 end
Note:
See TracChangeset
for help on using the changeset viewer.