summaryrefslogtreecommitdiff
path: root/dict.c
diff options
context:
space:
mode:
Diffstat (limited to 'dict.c')
-rw-r--r--dict.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/dict.c b/dict.c
index d6ccbca56e..fa4ffc04e3 100644
--- a/dict.c
+++ b/dict.c
@@ -41,7 +41,6 @@ static VALUE
Sdic_new(class)
VALUE class;
{
- int i, max;
NEWOBJ(dic, struct RDict);
OBJSETUP(dic, class, T_DICT);
@@ -50,6 +49,40 @@ Sdic_new(class)
return (VALUE)dic;
}
+static VALUE Fdic_clone();
+
+static VALUE
+Sdic_create(argc, argv, class)
+ int argc;
+ VALUE *argv;
+ VALUE class;
+{
+ struct RDict *dic;
+ int i;
+
+ if (argc == 1 && TYPE(argv[0]) == T_DICT) {
+ if (class == CLASS_OF(argv[0])) return argv[0];
+ else {
+ NEWOBJ(dic, struct RDict);
+ OBJSETUP(dic, class, T_DICT);
+ dic->tbl = (st_table*)st_copy(RDICT(argv[0])->tbl);
+
+ return (VALUE)dic;
+ }
+ }
+
+ if (argc % 2 != 0) {
+ Fail("odd number args for Dict");
+ }
+ dic = (struct RDict*)Sdic_new(class);
+
+ for (i=0; i<argc; i+=2) {
+ st_insert(dic->tbl, argv[i], argv[i+1]);
+ }
+
+ return (VALUE)dic;
+}
+
VALUE
dic_new()
{
@@ -551,6 +584,7 @@ Init_Dict()
rb_include_module(C_Dict, M_Enumerable);
rb_define_single_method(C_Dict, "new", Sdic_new, 0);
+ rb_define_single_method(C_Dict, "[]", Sdic_create, -1);
rb_define_method(C_Dict,"clone", Fdic_clone, 0);