diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-07-17 10:35:41 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-07-17 10:35:41 +0900 |
| commit | d605b72c1d6fa4564a0a5e88535504b6850463b5 (patch) | |
| tree | 774fc0de56002abb3bb2b1c3387ff08f91876d17 /mrbgems/mruby-hash-ext | |
| parent | 2af92d0ebcbeca6d3d85a27c8193273080a63090 (diff) | |
| parent | 9af3b7c6258de327218dd04e69d76ae68caf17b1 (diff) | |
| download | mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.tar.gz mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.zip | |
Merge branch 'master' into i110/inspect-recursion
Diffstat (limited to 'mrbgems/mruby-hash-ext')
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrblib/hash.rb | 89 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/src/hash-ext.c | 51 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/test/hash.rb | 6 |
3 files changed, 46 insertions, 100 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 549bca0a8..547f3404a 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -27,9 +27,9 @@ class Hash length = object.length if length == 1 o = object[0] - if o.respond_to?(:to_hash) + if Hash === o h = self.new - object[0].to_hash.each { |k, v| h[k] = v } + o.each { |k, v| h[k] = v } return h elsif o.respond_to?(:to_a) h = self.new @@ -62,25 +62,6 @@ class Hash ## # call-seq: - # Hash.try_convert(obj) -> hash or nil - # - # Try to convert <i>obj</i> into a hash, using to_hash method. - # Returns converted hash or nil if <i>obj</i> cannot be converted - # for any reason. - # - # Hash.try_convert({1=>2}) # => {1=>2} - # Hash.try_convert("1=>2") # => nil - # - def self.try_convert(obj) - if obj.respond_to?(:to_hash) - obj.to_hash - else - nil - end - end - - ## - # call-seq: # hsh.merge!(other_hash) -> hsh # hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh # @@ -101,7 +82,7 @@ class Hash # def merge!(other, &block) - raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash) + raise TypeError, "Hash required (#{other.class} given)" unless Hash === other if block other.each_key{|k| self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k] @@ -116,6 +97,31 @@ class Hash ## # call-seq: + # hsh.compact! -> hsh + # + # Removes all nil values from the hash. Returns the hash. + # Returns nil if the hash does not contain nil values. + # + # h = { a: 1, b: false, c: nil } + # h.compact! #=> { a: 1, b: false } + # + + def compact! + keys = self.keys + nk = keys.select{|k| + self[k] != nil + } + return nil if (keys.size == nk.size) + h = {} + nk.each {|k| + h[k] = self[k] + } + h + self.replace(h) + end + + ## + # call-seq: # hsh.compact -> new_hsh # # Returns a new hash with the nil values/key pairs removed @@ -125,9 +131,13 @@ class Hash # h #=> { a: 1, b: false, c: nil } # def compact - result = self.dup - result.compact! - result + h = {} + self.keys.select{|k| + self[k] != nil + }.each {|k| + h[k] = self[k] + } + h end ## @@ -300,11 +310,7 @@ class Hash # h1 < h1 #=> false # def <(hash) - begin - hash = hash.to_hash - rescue NoMethodError - raise TypeError, "can't convert #{hash.class} to Hash" - end + raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash size < hash.size and all? {|key, val| hash.key?(key) and hash[key] == val } @@ -324,11 +330,7 @@ class Hash # h1 <= h1 #=> true # def <=(hash) - begin - hash = hash.to_hash - rescue NoMethodError - raise TypeError, "can't convert #{hash.class} to Hash" - end + raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash size <= hash.size and all? {|key, val| hash.key?(key) and hash[key] == val } @@ -348,11 +350,7 @@ class Hash # h1 > h1 #=> false # def >(hash) - begin - hash = hash.to_hash - rescue NoMethodError - raise TypeError, "can't convert #{hash.class} to Hash" - end + raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash size > hash.size and hash.all? {|key, val| key?(key) and self[key] == val } @@ -372,11 +370,7 @@ class Hash # h1 >= h1 #=> true # def >=(hash) - begin - hash = hash.to_hash - rescue NoMethodError - raise TypeError, "can't convert #{hash.class} to Hash" - end + raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash size >= hash.size and hash.all? {|key, val| key?(key) and self[key] == val } @@ -432,9 +426,9 @@ class Hash return to_enum :transform_keys! unless block self.keys.each do |k| value = self[k] - new_key = block.call(k) self.__delete(k) - self[new_key] = value + k = block.call(k) if block + self[k] = value end self end @@ -457,6 +451,7 @@ class Hash end hash end + ## # call-seq: # hsh.transform_values! {|key| block } -> hsh diff --git a/mrbgems/mruby-hash-ext/src/hash-ext.c b/mrbgems/mruby-hash-ext/src/hash-ext.c index 6619f5268..e6112667b 100644 --- a/mrbgems/mruby-hash-ext/src/hash-ext.c +++ b/mrbgems/mruby-hash-ext/src/hash-ext.c @@ -37,42 +37,6 @@ hash_values_at(mrb_state *mrb, mrb_value hash) } /* - * call-seq: - * hsh.compact! -> hsh - * - * Removes all nil values from the hash. Returns the hash. - * - * h = { a: 1, b: false, c: nil } - * h.compact! #=> { a: 1, b: false } - */ -static mrb_value -hash_compact_bang(mrb_state *mrb, mrb_value hash) -{ - khiter_t k; - khash_t(ht) *h = RHASH_TBL(hash); - mrb_int n = -1; - - if (!h) return mrb_nil_value(); - for (k = kh_begin(h); k != kh_end(h); k++) { - if (kh_exist(h, k)) { - mrb_value val = kh_value(h, k).v; - khiter_t k2; - - if (mrb_nil_p(val)) { - kh_del(ht, mrb, h, k); - n = kh_value(h, k).n; - for (k2 = kh_begin(h); k2 != kh_end(h); k2++) { - if (!kh_exist(h, k2)) continue; - if (kh_value(h, k2).n > n) kh_value(h, k2).n--; - } - } - } - } - if (n < 0) return mrb_nil_value(); - return hash; -} - -/* * call-seq: * hsh.slice(*keys) -> a_hash * @@ -85,28 +49,22 @@ hash_compact_bang(mrb_state *mrb, mrb_value hash) static mrb_value hash_slice(mrb_state *mrb, mrb_value hash) { - khash_t(ht) *h = RHASH_TBL(hash); mrb_value *argv, result; mrb_int argc, i; - khiter_t k; - int ai; mrb_get_args(mrb, "*", &argv, &argc); - if (argc == 0 || h == NULL) { + if (argc == 0) { return mrb_hash_new_capa(mrb, argc); } result = mrb_hash_new_capa(mrb, argc); - ai = mrb_gc_arena_save(mrb); for (i = 0; i < argc; i++) { mrb_value key = argv[i]; + mrb_value val; - k = kh_get(ht, mrb, h, key); - if (k != kh_end(h)) { - mrb_value val = kh_value(h, k).v; - + val = mrb_hash_fetch(mrb, hash, key, mrb_undef_value()); + if (!mrb_undef_p(val)) { mrb_hash_set(mrb, result, key, val); } - mrb_gc_arena_restore(mrb, ai); } return result; } @@ -118,7 +76,6 @@ mrb_mruby_hash_ext_gem_init(mrb_state *mrb) h = mrb->hash_class; mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY()); - mrb_define_method(mrb, h, "compact!", hash_compact_bang, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "slice", hash_slice, MRB_ARGS_ANY()); } diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index 269da800d..b5d0aaaf8 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -45,12 +45,6 @@ assert('Hash.[] for sub class') do assert_equal(sub_hash_class, sub_hash.class) end -assert('Hash.try_convert') do - assert_nil Hash.try_convert(nil) - assert_nil Hash.try_convert("{1=>2}") - assert_equal({1=>2}, Hash.try_convert({1=>2})) -end - assert('Hash#merge!') do a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' } |
