mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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/trunk@13776 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d9ff0a6c22
commit
012b58b121
2 changed files with 32 additions and 8 deletions
|
@ -3,6 +3,12 @@ Fri Oct 26 01:48:28 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
* array.c (rb_ary_assoc): check and convert inner arrays (assocs)
|
* array.c (rb_ary_assoc): check and convert inner arrays (assocs)
|
||||||
using #to_ary.
|
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.
|
||||||
|
|
||||||
Thu Oct 25 14:19:33 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Thu Oct 25 14:19:33 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* io.c (rb_io_tell, rb_io_seek): check errno too. [ruby-dev:32093]
|
* io.c (rb_io_tell, rb_io_seek): check errno too. [ruby-dev:32093]
|
||||||
|
|
24
hash.c
24
hash.c
|
@ -21,6 +21,8 @@
|
||||||
#include <crt_externs.h>
|
#include <crt_externs.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static VALUE rb_hash_s_try_convert(VALUE, VALUE);
|
||||||
|
|
||||||
#define HASH_DELETED FL_USER1
|
#define HASH_DELETED FL_USER1
|
||||||
#define HASH_PROC_DEFAULT FL_USER2
|
#define HASH_PROC_DEFAULT FL_USER2
|
||||||
|
|
||||||
|
@ -315,18 +317,34 @@ rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
|
rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
VALUE hash;
|
VALUE hash, tmp;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (argc == 1 && TYPE(argv[0]) == T_HASH) {
|
if (argc == 1) {
|
||||||
|
tmp = rb_hash_s_try_convert(Qnil, argv[0]);
|
||||||
|
if (!NIL_P(tmp)) {
|
||||||
hash = hash_alloc(klass);
|
hash = hash_alloc(klass);
|
||||||
if (RHASH(argv[0])->ntbl) {
|
if (RHASH(argv[0])->ntbl) {
|
||||||
RHASH(hash)->ntbl = st_copy(RHASH(argv[0])->ntbl);
|
RHASH(hash)->ntbl = st_copy(RHASH(argv[0])->ntbl);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
if (argc % 2 != 0) {
|
||||||
rb_raise(rb_eArgError, "odd number of arguments for Hash");
|
rb_raise(rb_eArgError, "odd number of arguments for Hash");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue