Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 4 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/mrbgem.rake
r321 r439 3 3 spec.author = 'mruby developers' 4 4 spec.summary = 'Hash class extension' 5 spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext' 6 spec.add_dependency 'mruby-array-ext', :core => 'mruby-array-ext' 5 spec.add_dependency 'mruby-array-ext', core: 'mruby-array-ext' 7 6 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/mrblib/hash.rb
r331 r439 28 28 if length == 1 29 29 o = object[0] 30 if o.respond_to?(:to_hash)30 if Hash === o 31 31 h = self.new 32 o bject[0].to_hash.each { |k, v| h[k] = v }32 o.each { |k, v| h[k] = v } 33 33 return h 34 34 elsif o.respond_to?(:to_a) … … 63 63 ## 64 64 # call-seq: 65 # Hash.try_convert(obj) -> hash or nil66 #67 # Try to convert <i>obj</i> into a hash, using to_hash method.68 # Returns converted hash or nil if <i>obj</i> cannot be converted69 # for any reason.70 #71 # Hash.try_convert({1=>2}) # => {1=>2}72 # Hash.try_convert("1=>2") # => nil73 #74 def self.try_convert(obj)75 if obj.respond_to?(:to_hash)76 obj.to_hash77 else78 nil79 end80 end81 82 ##83 # call-seq:84 65 # hsh.merge!(other_hash) -> hsh 85 66 # hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh … … 102 83 103 84 def merge!(other, &block) 104 raise TypeError, " can't convert argument into Hash" unless other.respond_to?(:to_hash)85 raise TypeError, "Hash required (#{other.class} given)" unless Hash === other 105 86 if block 106 87 other.each_key{|k| … … 114 95 115 96 alias update merge! 97 98 ## 99 # call-seq: 100 # hsh.compact! -> hsh 101 # 102 # Removes all nil values from the hash. Returns the hash. 103 # Returns nil if the hash does not contain nil values. 104 # 105 # h = { a: 1, b: false, c: nil } 106 # h.compact! #=> { a: 1, b: false } 107 # 108 109 def compact! 110 keys = self.keys 111 nk = keys.select{|k| 112 self[k] != nil 113 } 114 return nil if (keys.size == nk.size) 115 h = {} 116 nk.each {|k| 117 h[k] = self[k] 118 } 119 h 120 self.replace(h) 121 end 122 123 ## 124 # call-seq: 125 # hsh.compact -> new_hsh 126 # 127 # Returns a new hash with the nil values/key pairs removed 128 # 129 # h = { a: 1, b: false, c: nil } 130 # h.compact #=> { a: 1, b: false } 131 # h #=> { a: 1, b: false, c: nil } 132 # 133 def compact 134 h = {} 135 self.keys.select{|k| 136 self[k] != nil 137 }.each {|k| 138 h[k] = self[k] 139 } 140 h 141 end 116 142 117 143 ## … … 150 176 none 151 177 else 152 raise KeyError, "Key not found: #{key }"178 raise KeyError, "Key not found: #{key.inspect}" 153 179 end 154 180 else … … 172 198 173 199 def delete_if(&block) 174 return to_enum :delete_if unless block _given?200 return to_enum :delete_if unless block 175 201 176 202 self.each do |k, v| … … 229 255 230 256 def keep_if(&block) 231 return to_enum :keep_if unless block _given?257 return to_enum :keep_if unless block 232 258 233 259 keys = [] … … 285 311 # 286 312 def <(hash) 287 begin 288 hash = hash.to_hash 289 rescue NoMethodError 290 raise TypeError, "can't convert #{hash.class} to Hash" 291 end 313 raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash 292 314 size < hash.size and all? {|key, val| 293 315 hash.key?(key) and hash[key] == val … … 309 331 # 310 332 def <=(hash) 311 begin 312 hash = hash.to_hash 313 rescue NoMethodError 314 raise TypeError, "can't convert #{hash.class} to Hash" 315 end 333 raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash 316 334 size <= hash.size and all? {|key, val| 317 335 hash.key?(key) and hash[key] == val … … 333 351 # 334 352 def >(hash) 335 begin 336 hash = hash.to_hash 337 rescue NoMethodError 338 raise TypeError, "can't convert #{hash.class} to Hash" 339 end 353 raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash 340 354 size > hash.size and hash.all? {|key, val| 341 355 key?(key) and self[key] == val … … 357 371 # 358 372 def >=(hash) 359 begin 360 hash = hash.to_hash 361 rescue NoMethodError 362 raise TypeError, "can't convert #{hash.class} to Hash" 363 end 373 raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash 364 374 size >= hash.size and hash.all? {|key, val| 365 375 key?(key) and self[key] == val … … 383 393 end 384 394 end 395 396 ## 397 # call-seq: 398 # hsh.transform_keys {|key| block } -> new_hash 399 # hsh.transform_keys -> an_enumerator 400 # 401 # Returns a new hash, with the keys computed from running the block 402 # once for each key in the hash, and the values unchanged. 403 # 404 # If no block is given, an enumerator is returned instead. 405 # 406 def transform_keys(&block) 407 return to_enum :transform_keys unless block 408 hash = {} 409 self.keys.each do |k| 410 new_key = block.call(k) 411 hash[new_key] = self[k] 412 end 413 hash 414 end 415 ## 416 # call-seq: 417 # hsh.transform_keys! {|key| block } -> hsh 418 # hsh.transform_keys! -> an_enumerator 419 # 420 # Invokes the given block once for each key in <i>hsh</i>, replacing it 421 # with the new key returned by the block, and then returns <i>hsh</i>. 422 # 423 # If no block is given, an enumerator is returned instead. 424 # 425 def transform_keys!(&block) 426 return to_enum :transform_keys! unless block 427 self.keys.each do |k| 428 value = self[k] 429 self.__delete(k) 430 k = block.call(k) if block 431 self[k] = value 432 end 433 self 434 end 435 ## 436 # call-seq: 437 # hsh.transform_values {|value| block } -> new_hash 438 # hsh.transform_values -> an_enumerator 439 # 440 # Returns a new hash with the results of running the block once for 441 # every value. 442 # This method does not change the keys. 443 # 444 # If no block is given, an enumerator is returned instead. 445 # 446 def transform_values(&b) 447 return to_enum :transform_values unless block_given? 448 hash = {} 449 self.keys.each do |k| 450 hash[k] = yield(self[k]) 451 end 452 hash 453 end 454 455 ## 456 # call-seq: 457 # hsh.transform_values! {|key| block } -> hsh 458 # hsh.transform_values! -> an_enumerator 459 # 460 # Invokes the given block once for each value in the hash, replacing 461 # with the new value returned by the block, and then returns <i>hsh</i>. 462 # 463 # If no block is given, an enumerator is returned instead. 464 # 465 def transform_values!(&b) 466 return to_enum :transform_values! unless block_given? 467 self.keys.each do |k| 468 self[k] = yield(self[k]) 469 end 470 self 471 end 472 473 def to_proc 474 ->x{self[x]} 475 end 476 477 ## 478 # call-seq: 479 # hsh.fetch_values(key, ...) -> array 480 # hsh.fetch_values(key, ...) { |key| block } -> array 481 # 482 # Returns an array containing the values associated with the given keys 483 # but also raises <code>KeyError</code> when one of keys can't be found. 484 # Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>. 485 # 486 # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } 487 # 488 # h.fetch_values("cow", "cat") #=> ["bovine", "feline"] 489 # h.fetch_values("cow", "bird") # raises KeyError 490 # h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"] 491 # 492 def fetch_values(*keys, &block) 493 keys.map do |k| 494 self.fetch(k, &block) 495 end 496 end 497 498 alias filter select 499 alias filter! select! 385 500 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/src/hash-ext.c
r331 r439 37 37 } 38 38 39 /* 40 * call-seq: 41 * hsh.slice(*keys) -> a_hash 42 * 43 * Returns a hash containing only the given keys and their values. 44 * 45 * h = { a: 100, b: 200, c: 300 } 46 * h.slice(:a) #=> {:a=>100} 47 * h.slice(:b, :c, :d) #=> {:b=>200, :c=>300} 48 */ 49 static mrb_value 50 hash_slice(mrb_state *mrb, mrb_value hash) 51 { 52 mrb_value *argv, result; 53 mrb_int argc, i; 54 55 mrb_get_args(mrb, "*", &argv, &argc); 56 result = mrb_hash_new_capa(mrb, argc); 57 if (argc == 0) return result; /* empty hash */ 58 for (i = 0; i < argc; i++) { 59 mrb_value key = argv[i]; 60 mrb_value val; 61 62 val = mrb_hash_fetch(mrb, hash, key, mrb_undef_value()); 63 if (!mrb_undef_p(val)) { 64 mrb_hash_set(mrb, result, key, val); 65 } 66 } 67 return result; 68 } 69 39 70 void 40 71 mrb_mruby_hash_ext_gem_init(mrb_state *mrb) … … 44 75 h = mrb->hash_class; 45 76 mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY()); 77 mrb_define_method(mrb, h, "slice", hash_slice, MRB_ARGS_ANY()); 46 78 } 47 79 -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/test/hash.rb
r331 r439 46 46 end 47 47 48 assert('Hash.try_convert') do49 assert_nil Hash.try_convert(nil)50 assert_nil Hash.try_convert("{1=>2}")51 assert_equal({1=>2}, Hash.try_convert({1=>2}))52 end53 54 48 assert('Hash#merge!') do 55 49 a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } … … 81 75 h = Hash.new { |hash,k| hash[k] = k } 82 76 assert_equal keys, h.values_at(*keys) 77 end 78 79 assert('Hash#compact') do 80 h = { "cat" => "feline", "dog" => nil, "cow" => false } 81 82 assert_equal({ "cat" => "feline", "cow" => false }, h.compact) 83 assert_equal({ "cat" => "feline", "dog" => nil, "cow" => false }, h) 84 end 85 86 assert('Hash#compact!') do 87 h = { "cat" => "feline", "dog" => nil, "cow" => false } 88 89 h.compact! 90 assert_equal({ "cat" => "feline", "cow" => false }, h) 83 91 end 84 92 … … 255 263 assert_nil(h.dig(:d)) 256 264 end 265 266 assert("Hash#transform_keys") do 267 h = {"1" => 100, "2" => 200} 268 assert_equal({"1!" => 100, "2!" => 200}, 269 h.transform_keys{|k| k+"!"}) 270 assert_equal({1 => 100, 2 => 200}, 271 h.transform_keys{|k|k.to_i}) 272 assert_same(h, h.transform_keys!{|k|k.to_i}) 273 assert_equal({1 => 100, 2 => 200}, h) 274 end 275 276 assert("Hash#transform_values") do 277 h = {a: 1, b: 2, c: 3} 278 assert_equal({a: 2, b: 5, c: 10}, 279 h.transform_values{|v| v * v + 1}) 280 assert_equal({a: "1", b: "2", c: "3"}, 281 h.transform_values{|v|v.to_s}) 282 assert_same(h, h.transform_values!{|v|v.to_s}) 283 assert_equal({a: "1", b: "2", c: "3"}, h) 284 end 285 286 assert("Hash#slice") do 287 h = { a: 100, b: 200, c: 300 } 288 assert_equal({:a=>100}, h.slice(:a)) 289 assert_equal({:b=>200, :c=>300}, h.slice(:b, :c, :d)) 290 end
Note:
See TracChangeset
for help on using the changeset viewer.