diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-05-07 22:50:24 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-05-07 22:50:24 -0700 |
| commit | c80103e89a7ddcbaa7b5fc470358a4bcae327953 (patch) | |
| tree | 4ebbc46f688f1d4c4e81eec715a9314ebb286c73 | |
| parent | 11e70f2191caecfe546f8a97baf284c1cfc25e00 (diff) | |
| parent | c9edc5b02ee07aa2b8caa994910d31273260ca22 (diff) | |
| download | mruby-c80103e89a7ddcbaa7b5fc470358a4bcae327953.tar.gz mruby-c80103e89a7ddcbaa7b5fc470358a4bcae327953.zip | |
Merge pull request #1237 from monaka/pr-cleanup-hash
Cleanup hash
| -rw-r--r-- | mrbgems/mruby-hash-ext/src/hash-ext.c | 62 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/test/hash.rb | 6 | ||||
| -rw-r--r-- | mrblib/hash.rb | 45 | ||||
| -rw-r--r-- | src/hash.c | 92 | ||||
| -rw-r--r-- | test/t/hash.rb | 15 |
5 files changed, 126 insertions, 94 deletions
diff --git a/mrbgems/mruby-hash-ext/src/hash-ext.c b/mrbgems/mruby-hash-ext/src/hash-ext.c new file mode 100644 index 000000000..94518924e --- /dev/null +++ b/mrbgems/mruby-hash-ext/src/hash-ext.c @@ -0,0 +1,62 @@ +/* +** hash.c - Hash class +** +** See Copyright Notice in mruby.h +*/ + +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/class.h" +#include "mruby/hash.h" +#include "mruby/khash.h" +#include "mruby/string.h" +#include "mruby/variable.h" + +/* + * call-seq: + * hsh.values_at(key, ...) -> array + * + * Return an array containing the values associated with the given keys. + * Also see <code>Hash.select</code>. + * + * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + * h.values_at("cow", "cat") #=> ["bovine", "feline"] + */ + +mrb_value +mrb_hash_values_at(mrb_state *mrb, int argc, mrb_value *argv, mrb_value hash) +{ + mrb_value result = mrb_ary_new_capa(mrb, argc); + long i; + + for (i=0; i<argc; i++) { + mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i])); + } + return result; +} + +static mrb_value +hash_values_at(mrb_state *mrb, mrb_value hash) +{ + mrb_value *argv; + int argc; + + mrb_get_args(mrb, "*", &argv, &argc); + + return mrb_hash_values_at(mrb, argc, argv, hash); +} + +void +mrb_mruby_hash_ext_gem_init(mrb_state *mrb) +{ + struct RClass *h; + + h = mrb->hash_class; + + mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY()); +} + +void +mrb_mruby_hash_ext_gem_final(mrb_state *mrb) +{ +} diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index 98eb313a4..73e12d8f2 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -18,3 +18,9 @@ assert('Hash#merge!') do 'xyz_key' => 'xyz_value' } end +assert('Hash#values_at') do + h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + result = h.values_at("cow", "cat") + + result == ["bovine", "feline"] +end diff --git a/mrblib/hash.rb b/mrblib/hash.rb index c304d4845..f7cdbdc6d 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -24,6 +24,23 @@ class Hash # Calls the given block for each element of +self+ # and pass the key and value of each element. # + # call-seq: + # hsh.each {| key, value | block } -> hsh + # hsh.each_pair {| key, value | block } -> hsh + # hsh.each -> an_enumerator + # hsh.each_pair -> an_enumerator + # + # + # If no block is given, an enumerator is returned instead. + # + # h = { "a" => 100, "b" => 200 } + # h.each {|key, value| puts "#{key} is #{value}" } + # + # <em>produces:</em> + # + # a is 100 + # b is 200 + # # ISO 15.2.13.4.9 def each(&block) self.keys.each{|k| block.call([k, self[k]])} @@ -34,6 +51,20 @@ class Hash # Calls the given block for each element of +self+ # and pass the key of each element. # + # call-seq: + # hsh.each_key {| key | block } -> hsh + # hsh.each_key -> an_enumerator + # + # If no block is given, an enumerator is returned instead. + # + # h = { "a" => 100, "b" => 200 } + # h.each_key {|key| puts key } + # + # <em>produces:</em> + # + # a + # b + # # ISO 15.2.13.4.10 def each_key(&block) self.keys.each{|k| block.call(k)} @@ -44,6 +75,20 @@ class Hash # Calls the given block for each element of +self+ # and pass the value of each element. # + # call-seq: + # hsh.each_value {| value | block } -> hsh + # hsh.each_value -> an_enumerator + # + # If no block is given, an enumerator is returned instead. + # + # h = { "a" => 100, "b" => 200 } + # h.each_value {|value| puts value } + # + # <em>produces:</em> + # + # 100 + # 200 + # # ISO 15.2.13.4.11 def each_value(&block) self.keys.each{|k| block.call(self[k])} diff --git a/src/hash.c b/src/hash.c index da5f49b02..882dd3121 100644 --- a/src/hash.c +++ b/src/hash.c @@ -324,12 +324,6 @@ mrb_hash_aget(mrb_state *mrb, mrb_value self) return mrb_hash_get(mrb, self, key); } -mrb_value -mrb_hash_lookup(mrb_state *mrb, mrb_value hash, mrb_value key) -{ - return mrb_hash_get(mrb, hash, key); -} - /* * call-seq: * hsh.fetch(key [, default] ) -> obj @@ -611,29 +605,6 @@ mrb_hash_shift(mrb_state *mrb, mrb_value hash) */ /* - * call-seq: - * hsh.values_at(key, ...) -> array - * - * Return an array containing the values associated with the given keys. - * Also see <code>Hash.select</code>. - * - * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } - * h.values_at("cow", "cat") #=> ["bovine", "feline"] - */ - -mrb_value -mrb_hash_values_at(mrb_state *mrb, int argc, mrb_value *argv, mrb_value hash) -{ - mrb_value result = mrb_ary_new_capa(mrb, argc); - long i; - - for (i=0; i<argc; i++) { - mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i])); - } - return result; -} - -/* * call-seq: * hsh.select {|key, value| block} -> a_hash * hsh.select -> an_enumerator @@ -813,69 +784,6 @@ mrb_hash_empty_p(mrb_state *mrb, mrb_value self) return mrb_bool_value(empty_p); } -/* 15.2.13.4.11 */ -/* - * call-seq: - * hsh.each_value {| value | block } -> hsh - * hsh.each_value -> an_enumerator - * - * Calls <i>block</i> once for each key in <i>hsh</i>, passing the - * value as a parameter. - * - * If no block is given, an enumerator is returned instead. - * - * h = { "a" => 100, "b" => 200 } - * h.each_value {|value| puts value } - * - * <em>produces:</em> - * - * 100 - * 200 - */ - -/* 15.2.13.4.10 */ -/* - * call-seq: - * hsh.each_key {| key | block } -> hsh - * hsh.each_key -> an_enumerator - * - * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key - * as a parameter. - * - * If no block is given, an enumerator is returned instead. - * - * h = { "a" => 100, "b" => 200 } - * h.each_key {|key| puts key } - * - * <em>produces:</em> - * - * a - * b - */ - -/* 15.2.13.4.9 */ -/* - * call-seq: - * hsh.each {| key, value | block } -> hsh - * hsh.each_pair {| key, value | block } -> hsh - * hsh.each -> an_enumerator - * hsh.each_pair -> an_enumerator - * - * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value - * pair as parameters. - * - * If no block is given, an enumerator is returned instead. - * - * h = { "a" => 100, "b" => 200 } - * h.each {|key, value| puts "#{key} is #{value}" } - * - * <em>produces:</em> - * - * a is 100 - * b is 200 - * - */ - static mrb_value inspect_hash(mrb_state *mrb, mrb_value hash, int recur) { diff --git a/test/t/hash.rb b/test/t/hash.rb index 5ba476e40..ef03c4a26 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -52,9 +52,12 @@ end assert('Hash#default_proc', '15.2.13.4.7') do a = Hash.new - b = Hash.new {|s,k| s[k] = k} + b = Hash.new {|s,k| s[k] = k + k} + c = b[2] + d = b['cat'] - a.default_proc == nil and b.default_proc.class == Proc + a.default_proc == nil and b.default_proc.class == Proc and + c = 4 and d = 'catcat' end assert('Hash#delete', '15.2.13.4.8') do @@ -278,3 +281,11 @@ assert('Hash#select!') do h == {:two => 2, :four => 4} end +# Not ISO specified + +assert('Hash#inspect') do + h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 } + ret = h.to_s + + ret = "{\"c\"=>300, \"a\"=>100, \"d\"=>400}" +end |
