summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-02 06:30:26 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-11-02 06:30:26 +0000
commit35c9feaeb72c1bf6c8642d05e9b0974649d12651 (patch)
treedb8464e9067708ad180f96b08a63c8bbf091e9e3 /hash.c
parenta6157194a63e8e254f94b4ee3b7c9184b34aa3fc (diff)
* array.c (rb_ary_assoc): check and convert inner arrays (assocs)
using #to_ary. * hash.c (rb_hash_s_create): check and convert argument hash using #to_hash. * hash.c (rb_hash_s_create): Hash#[] now takes assocs as source of hash conversion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@13803 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/hash.c b/hash.c
index 8b33a54b56..0878875ed3 100644
--- a/hash.c
+++ b/hash.c
@@ -336,16 +336,32 @@ rb_hash_s_create(argc, argv, klass)
VALUE *argv;
VALUE klass;
{
- VALUE hash;
+ VALUE hash, tmp;
int i;
- if (argc == 1 && TYPE(argv[0]) == T_HASH) {
- hash = hash_alloc0(klass);
- RHASH(hash)->tbl = st_copy(RHASH(argv[0])->tbl);
+ if (argc == 1) {
+ tmp = rb_check_convert_type(argv[0], T_HASH, "Hash", "to_hash");
+ if (!NIL_P(tmp)) {
+ hash = hash_alloc0(klass);
+ RHASH(hash)->tbl = st_copy(RHASH(tmp)->tbl);
+ return hash;
+ }
- return hash;
+ tmp = rb_check_array_type(argv[0]);
+ if (!NIL_P(tmp)) {
+ long i;
+
+ hash = hash_alloc(klass);
+ for (i = 0; i < RARRAY_LEN(tmp); ++i) {
+ VALUE v = rb_check_array_type(RARRAY_PTR(tmp)[i]);
+
+ if (NIL_P(v)) continue;
+ if (RARRAY_LEN(v) < 1 || 2 < RARRAY_LEN(v)) continue;
+ rb_hash_aset(hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]);
+ }
+ return hash;
+ }
}
-
if (argc % 2 != 0) {
rb_raise(rb_eArgError, "odd number of arguments for Hash");
}