diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-09 07:17:03 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-09 07:17:03 +0000 |
commit | 6d483e5b90c4ec195bbba37464f041835d0386a8 (patch) | |
tree | 9e1e2f887594008ced542435b04aee5aab76afd6 | |
parent | 70d2b98dde0a0b8768026c983aba7cdd7fb2c52d (diff) | |
download | ruby-6d483e5b90c4ec195bbba37464f041835d0386a8.tar.gz |
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
-rw-r--r-- | hash.c | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -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; } |