summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMasaki Muranaka <[email protected]>2013-05-08 11:58:46 +0900
committerMasaki Muranaka <[email protected]>2013-05-08 13:22:35 +0900
commita46eacb213bbf001f688dc8f00cf85ea1b8a6281 (patch)
treeb6158eb0863a7341ee4c46f0152234568d79cdeb
parent11e70f2191caecfe546f8a97baf284c1cfc25e00 (diff)
downloadmruby-a46eacb213bbf001f688dc8f00cf85ea1b8a6281.tar.gz
mruby-a46eacb213bbf001f688dc8f00cf85ea1b8a6281.zip
Move Hash#values_at to mruby-hash-ext gem.
-rw-r--r--mrbgems/mruby-hash-ext/src/hash-ext.c62
-rw-r--r--mrbgems/mruby-hash-ext/test/hash.rb6
-rw-r--r--src/hash.c23
3 files changed, 68 insertions, 23 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/src/hash.c b/src/hash.c
index da5f49b02..566d423a9 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -611,29 +611,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