1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

hash.c: SET_PROC_DEFAULT

* hash.c (SET_PROC_DEFAULT): new macro to set the default proc.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54058 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-03-09 07:17:03 +00:00
parent 7522e06424
commit e684069042

25
hash.c
View file

@ -36,6 +36,8 @@
FL_UNSET_RAW(hash, HASH_PROC_DEFAULT), \
RHASH_SET_IFNONE(hash, ifnone))
#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
static VALUE
has_extra_methods(VALUE klass)
{
@ -528,14 +530,19 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
static void
default_proc_arity_check(VALUE proc)
set_proc_default(VALUE hash, VALUE proc)
{
int n = rb_proc_arity(proc);
if (rb_proc_lambda_p(proc)) {
int n = rb_proc_arity(proc);
if (rb_proc_lambda_p(proc) && n != 2 && (n >= 0 || n < -3)) {
if (n < 0) n = -n-1;
rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
if (n != 2 && (n >= 0 || n < -3)) {
if (n < 0) n = -n-1;
rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
}
}
FL_SET_RAW(hash, HASH_PROC_DEFAULT);
RHASH_SET_IFNONE(hash, proc);
}
/*
@ -582,9 +589,7 @@ rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
if (rb_block_given_p()) {
rb_check_arity(argc, 0, 0);
ifnone = rb_block_proc();
default_proc_arity_check(ifnone);
RHASH_SET_IFNONE(hash, ifnone);
FL_SET(hash, HASH_PROC_DEFAULT);
SET_PROC_DEFAULT(hash, ifnone);
}
else {
rb_check_arity(argc, 0, 1);
@ -1006,9 +1011,7 @@ rb_hash_set_default_proc(VALUE hash, VALUE proc)
rb_obj_classname(proc));
}
proc = b;
default_proc_arity_check(proc);
RHASH_SET_IFNONE(hash, proc);
FL_SET(hash, HASH_PROC_DEFAULT);
SET_PROC_DEFAULT(hash, proc);
return proc;
}