diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-26 10:30:15 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-26 10:30:15 +0000 |
commit | 121b2b2802a3219821c23435f9ca8db6ff89aac1 (patch) | |
tree | 7a3ae77a5876cd725dc4bb6bea53df686d373318 | |
parent | b662c25b597e9fc8be0e1db7a1337445a224c312 (diff) | |
download | ruby-121b2b2802a3219821c23435f9ca8db6ff89aac1.tar.gz ruby-121b2b2802a3219821c23435f9ca8db6ff89aac1.tar.xz ruby-121b2b2802a3219821c23435f9ca8db6ff89aac1.zip |
* hash.c (default_proc_arity_check): new support function.
* hash.c (rb_hash_initialize): should do arity check as #default_proc=.
[ruby-core:26281]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25490 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | hash.c | 22 |
2 files changed, 22 insertions, 7 deletions
@@ -1,3 +1,10 @@ +Mon Oct 26 18:37:57 2009 Yukihiro Matsumoto <[email protected]> + + * hash.c (default_proc_arity_check): new support function. + + * hash.c (rb_hash_initialize): should do arity check as #default_proc=. + [ruby-core:26281] + Mon Oct 26 13:24:17 2009 Hidetoshi NAGAI <[email protected]> * ext/tk/lib/remote-tk.rb: typo fixed. @@ -268,6 +268,17 @@ rb_hash_modify(VALUE hash) rb_hash_tbl(hash); } +static void +default_proc_arity_check(VALUE 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); + } +} + /* * call-seq: * Hash.new => hash @@ -313,7 +324,9 @@ rb_hash_initialize(int argc, VALUE *argv, VALUE hash) if (argc > 0) { rb_raise(rb_eArgError, "wrong number of arguments"); } - RHASH(hash)->ifnone = rb_block_proc(); + ifnone = rb_block_proc(); + default_proc_arity_check(ifnone); + RHASH(hash)->ifnone = ifnone; FL_SET(hash, HASH_PROC_DEFAULT); } else { @@ -667,7 +680,6 @@ static VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc) { VALUE b; - int n; rb_hash_modify(hash); b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc"); @@ -677,11 +689,7 @@ rb_hash_set_default_proc(VALUE hash, VALUE proc) rb_obj_classname(proc)); } proc = b; - 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); - } + default_proc_arity_check(proc); RHASH(hash)->ifnone = proc; FL_SET(hash, HASH_PROC_DEFAULT); return proc; |